Update app.py
Browse files
app.py
CHANGED
|
@@ -13,80 +13,80 @@ st.set_page_config(page_title="Fortuen Stick Enquiry", layout="wide")
|
|
| 13 |
|
| 14 |
st.title("Fortuen Stick Enquiry")
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
|
| 90 |
|
| 91 |
# # ---- Left Column ----
|
| 92 |
# with left_col:
|
|
|
|
| 13 |
|
| 14 |
st.title("Fortuen Stick Enquiry")
|
| 15 |
|
| 16 |
+
# Initialize session state variables
|
| 17 |
+
if "submitted_text" not in st.session_state:
|
| 18 |
+
st.session_state.submitted_text = False
|
| 19 |
+
if "fortune_number" not in st.session_state:
|
| 20 |
+
st.session_state.fortune_number = None
|
| 21 |
+
if "fortune_row" not in st.session_state:
|
| 22 |
+
st.session_state.fortune_row = None
|
| 23 |
+
if "fortune_data" not in st.session_state:
|
| 24 |
+
try:
|
| 25 |
+
st.session_state.fortune_data = pd.read_csv("detail.csv")
|
| 26 |
+
except Exception as e:
|
| 27 |
+
st.error(f"Error loading CSV: {e}")
|
| 28 |
+
st.session_state.fortune_data = None
|
| 29 |
|
| 30 |
+
if "stick_clicked" not in st.session_state:
|
| 31 |
+
st.session_state.stick_clicked = False
|
| 32 |
|
| 33 |
|
| 34 |
+
# Callback function for the submit button:
|
| 35 |
+
def submit_text_callback():
|
| 36 |
+
st.session_state.submitted_text = True
|
| 37 |
+
# Randomly generate a number from 1 to 100
|
| 38 |
+
st.session_state.fortune_number = random.randint(1, 100)
|
| 39 |
|
| 40 |
+
# Look up the row in the CSV where CNumber matches the generated fortune number.
|
| 41 |
+
df = st.session_state.fortune_data
|
| 42 |
+
if df is not None:
|
| 43 |
+
matching_row = df[df['CNumber'] == st.session_state.fortune_number]
|
| 44 |
+
if not matching_row.empty:
|
| 45 |
+
row = matching_row.iloc[0]
|
| 46 |
+
st.session_state.fortune_row = {
|
| 47 |
+
"Header": row.get("Header", "N/A"),
|
| 48 |
+
"Luck": row.get("Luck", "N/A"),
|
| 49 |
+
"Description": row.get("Description", "No description available."),
|
| 50 |
+
"Detail": row.get("Detail", "No detail available."),
|
| 51 |
+
"HeaderLink": row.get("link", None) # URL to the image
|
| 52 |
+
}
|
| 53 |
+
else:
|
| 54 |
+
st.session_state.fortune_row = {
|
| 55 |
+
"Header": "N/A",
|
| 56 |
+
"Luck": "N/A",
|
| 57 |
+
"Description": "No description available.",
|
| 58 |
+
"Detail": "No detail available.",
|
| 59 |
+
"HeaderLink": None
|
| 60 |
+
}
|
| 61 |
|
| 62 |
+
# Function to load and resize local images using Pillow
|
| 63 |
+
def load_and_resize_image(path, max_size=MAX_SIZE):
|
| 64 |
+
try:
|
| 65 |
+
img = Image.open(path)
|
| 66 |
+
img.thumbnail(max_size, Image.Resampling.LANCZOS)
|
| 67 |
+
return img
|
| 68 |
+
except Exception as e:
|
| 69 |
+
st.error(f"Error loading image: {e}")
|
| 70 |
+
return None
|
| 71 |
|
| 72 |
+
# Function to download image from URL and resize it
|
| 73 |
+
def download_and_resize_image(url, max_size=MAX_SIZE):
|
| 74 |
+
try:
|
| 75 |
+
response = requests.get(url)
|
| 76 |
+
response.raise_for_status()
|
| 77 |
+
image_bytes = BytesIO(response.content)
|
| 78 |
+
img = Image.open(image_bytes)
|
| 79 |
+
img.thumbnail(max_size, Image.Resampling.LANCZOS)
|
| 80 |
+
return img
|
| 81 |
+
except Exception as e:
|
| 82 |
+
st.error(f"Error loading image from URL: {e}")
|
| 83 |
+
return None
|
| 84 |
|
| 85 |
+
def stick_enquiry_callback():
|
| 86 |
+
st.session_state.stick_clicked = True
|
| 87 |
|
| 88 |
+
# Main layout: Left (input) and Right (fortune display)
|
| 89 |
+
left_col, _, right_col = st.columns([3, 1, 5])
|
| 90 |
|
| 91 |
# # ---- Left Column ----
|
| 92 |
# with left_col:
|