Ashendilantha commited on
Commit
6e49692
·
verified ·
1 Parent(s): f7e24e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -9
app.py CHANGED
@@ -8,22 +8,31 @@ from nltk.stem import WordNetLemmatizer
8
  from transformers import pipeline
9
  from PIL import Image
10
 
11
- # Ensure set_page_config is the first Streamlit command
12
- if "device_type" not in st.session_state:
13
- st.set_page_config(page_title="News Classifier", page_icon="📰", layout="wide") # Default layout
14
 
15
- # Ask user for device type
16
- device_type = st.radio("Are you using a mobile device or a PC?", ["Mobile", "PC"])
 
 
 
 
 
 
 
 
 
 
17
  st.session_state.device_type = device_type
18
- st.rerun() # Corrected method for refreshing the app
19
 
20
- # Apply the selected layout before anything else
21
- if st.session_state.device_type == "Mobile":
22
  st.set_page_config(page_title="News Classifier", page_icon="📰", layout="centered")
23
  else:
24
  st.set_page_config(page_title="News Classifier", page_icon="📰", layout="wide")
25
 
26
- # Now continue with the rest of your code
27
  nltk.download('stopwords')
28
  nltk.download('wordnet')
29
  nltk.download('omw-1.4')
 
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')