Ashendilantha commited on
Commit
613983d
Β·
verified Β·
1 Parent(s): 6e49692

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -22
app.py CHANGED
@@ -8,31 +8,34 @@ from nltk.stem import WordNetLemmatizer
8
  from transformers import pipeline
9
  from PIL import Image
10
 
11
- # JavaScript to detect screen width
12
- def get_device_type():
13
- return st.session_state.get("device_type", None)
14
-
 
 
 
 
 
 
 
 
 
 
 
15
  if "device_type" not in st.session_state:
16
- device_type_script = """
17
- <script>
18
- let screen_width = window.innerWidth;
19
- let device_type = screen_width < 768 ? "Mobile" : "PC";
20
- window.parent.postMessage(device_type, "*");
21
- </script>
22
- """
23
- st.markdown(device_type_script, unsafe_allow_html=True)
24
-
25
- # Wait for response from JavaScript
26
- device_type = st.selectbox("Select your device type", ["Mobile", "PC"])
27
- st.session_state.device_type = device_type
28
-
29
- # Set the layout only ONCE at the start
30
- if get_device_type() == "Mobile":
31
- st.set_page_config(page_title="News Classifier", page_icon="πŸ“°", layout="centered")
32
  else:
33
- st.set_page_config(page_title="News Classifier", page_icon="πŸ“°", layout="wide")
34
 
35
- # Now continue with the rest of the app
36
  nltk.download('stopwords')
37
  nltk.download('wordnet')
38
  nltk.download('omw-1.4')
 
8
  from transformers import pipeline
9
  from PIL import Image
10
 
11
+ # βœ… Set page config as the first Streamlit command (default to wide)
12
+ st.set_page_config(page_title="News Classifier", page_icon="πŸ“°", layout="wide")
13
+
14
+ # βœ… Use JavaScript to detect screen size and update session state
15
+ device_type_script = """
16
+ <script>
17
+ let screen_width = window.innerWidth;
18
+ let device_type = screen_width < 768 ? "Mobile" : "PC";
19
+ let streamlitVar = window.parent;
20
+ streamlitVar.postMessage({device_type: device_type}, "*");
21
+ </script>
22
+ """
23
+ st.markdown(device_type_script, unsafe_allow_html=True)
24
+
25
+ # βœ… Initialize session state
26
  if "device_type" not in st.session_state:
27
+ st.session_state.device_type = "PC" # Default to PC
28
+
29
+ # βœ… Allow user to manually select device type (Optional)
30
+ st.session_state.device_type = st.radio("Select your device type", ["Mobile", "PC"], index=0 if st.session_state.device_type == "PC" else 1)
31
+
32
+ # βœ… Apply UI adjustments based on device type
33
+ if st.session_state.device_type == "Mobile":
34
+ st.write("πŸ“± Mobile Mode Activated (UI adjusted)")
 
 
 
 
 
 
 
 
35
  else:
36
+ st.write("πŸ’» PC Mode Activated (Full-width UI)")
37
 
38
+ # βœ… Continue with the rest of the app
39
  nltk.download('stopwords')
40
  nltk.download('wordnet')
41
  nltk.download('omw-1.4')