Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import time
|
| 3 |
import random
|
| 4 |
import pandas as pd
|
| 5 |
import requests
|
|
@@ -14,153 +13,153 @@ st.set_page_config(page_title="Fortuen Stick Enquiry", layout="wide")
|
|
| 14 |
|
| 15 |
st.title("Fortuen Stick Enquiry")
|
| 16 |
|
| 17 |
-
# Initialize session state variables
|
| 18 |
-
if "submitted_text" not in st.session_state:
|
| 19 |
-
|
| 20 |
-
if "fortune_number" not in st.session_state:
|
| 21 |
-
|
| 22 |
-
if "fortune_row" not in st.session_state:
|
| 23 |
-
|
| 24 |
-
if "fortune_data" not in st.session_state:
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
| 31 |
-
if "stick_clicked" not in st.session_state:
|
| 32 |
-
|
| 33 |
|
| 34 |
|
| 35 |
-
# Callback function for the submit button:
|
| 36 |
-
def submit_text_callback():
|
| 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 |
-
# Function to load and resize local images using Pillow
|
| 64 |
-
def load_and_resize_image(path, max_size=MAX_SIZE):
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
|
| 73 |
-
# Function to download image from URL and resize it
|
| 74 |
-
def download_and_resize_image(url, max_size=MAX_SIZE):
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
|
| 86 |
-
def stick_enquiry_callback():
|
| 87 |
-
|
| 88 |
|
| 89 |
-
# Main layout: Left (input) and Right (fortune display)
|
| 90 |
-
left_col, _, right_col = st.columns([3, 1, 5])
|
| 91 |
|
| 92 |
-
# ---- Left Column ----
|
| 93 |
-
with left_col:
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
|
| 116 |
-
# ---- Right Column ----
|
| 117 |
-
with right_col:
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import random
|
| 3 |
import pandas as pd
|
| 4 |
import requests
|
|
|
|
| 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:
|
| 93 |
+
# left_top = st.container()
|
| 94 |
+
# left_bottom = st.container()
|
| 95 |
+
# # Top container: Input area and submit button
|
| 96 |
+
# with left_top:
|
| 97 |
+
# user_sentence = st.text_area("Enter your question in English", key="user_sentence", height=150)
|
| 98 |
|
| 99 |
+
# st.button("submit", key="submit_button", on_click=submit_text_callback)
|
| 100 |
+
# # (The previous Stick Enquiry button has been removed)
|
| 101 |
|
| 102 |
+
# if st.session_state.submitted_text:
|
| 103 |
+
# # Left Bottom: Centered "解籤/Stick Enquiry" button
|
| 104 |
+
# with left_bottom:
|
| 105 |
+
# # Add vertical spacing to approximately center the button vertically
|
| 106 |
+
# for _ in range(5):
|
| 107 |
+
# st.write("")
|
| 108 |
+
# col1, col2, col3 = st.columns(3)
|
| 109 |
+
# with col2:
|
| 110 |
+
# st.button("Cfu Explain", key="stick_button", on_click=stick_enquiry_callback)
|
| 111 |
+
# if st.session_state.stick_clicked:
|
| 112 |
+
# # st.write("Here are the stick enquiry words...") # Placeholder text
|
| 113 |
+
# st.text_area(' ', value="Here are the stick enquiry words...", height=300, disabled=True)
|
| 114 |
|
| 115 |
+
# # ---- Right Column ----
|
| 116 |
+
# with right_col:
|
| 117 |
+
# # Top container: Fortune image area
|
| 118 |
+
# with st.container():
|
| 119 |
+
# col_left, col_center, col_right = st.columns([1, 2, 1])
|
| 120 |
+
# with col_center:
|
| 121 |
+
# if st.session_state.submitted_text and st.session_state.fortune_row:
|
| 122 |
+
# header_link = st.session_state.fortune_row.get("HeaderLink")
|
| 123 |
+
# if header_link:
|
| 124 |
+
# # Download the image from the URL
|
| 125 |
+
# img_from_url = download_and_resize_image(header_link)
|
| 126 |
+
# if img_from_url:
|
| 127 |
+
# st.image(img_from_url, use_container_width=False)
|
| 128 |
+
# else:
|
| 129 |
+
# # Fallback: display a default image if download fails
|
| 130 |
+
# img = load_and_resize_image("error.png")
|
| 131 |
+
# if img:
|
| 132 |
+
# st.image(img, use_container_width=False)
|
| 133 |
+
# else:
|
| 134 |
+
# # Fallback: display a default image if no header link is provided
|
| 135 |
+
# img = load_and_resize_image("error.png")
|
| 136 |
+
# if img:
|
| 137 |
+
# st.image(img, use_container_width=False)
|
| 138 |
+
# else:
|
| 139 |
+
# # Before submit, show the default image
|
| 140 |
+
# img = load_and_resize_image("fortune.png")
|
| 141 |
+
# if img:
|
| 142 |
+
# st.image(img, caption="Your Fortune", use_container_width=False)
|
| 143 |
|
| 144 |
+
# # Bottom container: Display fortune details using text areas
|
| 145 |
+
# with st.container():
|
| 146 |
+
# if st.session_state.fortune_row:
|
| 147 |
+
# header_text = st.session_state.fortune_row.get("Header", "N/A")
|
| 148 |
+
# luck_text = st.session_state.fortune_row.get("Luck", "N/A")
|
| 149 |
+
# description_text = st.session_state.fortune_row.get("Description", "No description available.")
|
| 150 |
+
# detail_text = st.session_state.fortune_row.get("Detail", "No detail available.")
|
| 151 |
|
| 152 |
+
# # Create a summary with larger text using HTML styling
|
| 153 |
+
# summary = f"""
|
| 154 |
+
# <div style="font-size: 28px; font-weight: bold;">
|
| 155 |
+
# Fortune stick number: {st.session_state.fortune_number}<br>
|
| 156 |
+
# Luck: {luck_text}
|
| 157 |
+
# </div>
|
| 158 |
+
# """
|
| 159 |
+
# st.markdown(summary, unsafe_allow_html=True)
|
| 160 |
|
| 161 |
+
# # Second text area: Description
|
| 162 |
+
# st.text_area("Description", value=description_text, height=150, disabled=True)
|
| 163 |
|
| 164 |
+
# # Third text area: Detail
|
| 165 |
+
# st.text_area("Detail", value=detail_text, height=150, disabled=True)
|