Danial7 commited on
Commit
29d7742
·
verified ·
1 Parent(s): d4f8d36

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -28
app.py CHANGED
@@ -4,18 +4,18 @@ import matplotlib.pyplot as plt
4
  import random
5
  import time
6
 
7
- st.set_page_config(page_title="WiFi Signal Analyzer", layout="centered")
8
- st.title("📶 Real-Time WiFi Signal Analyzer")
9
 
10
- st.write("👣 Walk around and monitor WiFi strength in real time (simulated for demo).")
11
 
12
- # Initialize data store
13
  if "tracking" not in st.session_state:
14
  st.session_state.tracking = False
15
  if "data" not in st.session_state:
16
  st.session_state.data = []
17
 
18
- # Start/Stop tracking
19
  col1, col2 = st.columns(2)
20
  with col1:
21
  if st.button("▶️ Start Tracking", disabled=st.session_state.tracking):
@@ -24,37 +24,56 @@ with col2:
24
  if st.button("⏹ Stop Tracking", disabled=not st.session_state.tracking):
25
  st.session_state.tracking = False
26
 
27
- # Real-time update (simulated)
28
- placeholder = st.empty()
29
- graph_placeholder = st.empty()
30
- best_signal_placeholder = st.empty()
31
 
 
32
  while st.session_state.tracking:
33
- # Simulate a new signal
34
- signal = random.randint(-90, -30)
35
  position = len(st.session_state.data) + 1
36
- st.session_state.data.append({
37
- "Position": position,
38
- "Signal Strength (dBm)": signal
39
- })
40
 
41
- # Create dataframe
42
  df = pd.DataFrame(st.session_state.data)
43
 
44
- # Display current signal
45
- placeholder.markdown(f"📍 **Current Signal at Position {position}: `{signal} dBm`**")
 
 
 
 
 
 
46
 
47
- # Line chart
48
- graph_placeholder.line_chart(df.set_index("Position"))
49
 
50
- # Show best signal
51
- best_row = df[df["Signal Strength (dBm)"] == df["Signal Strength (dBm)"].max()]
52
- best_signal_placeholder.markdown(f"✅ **Best Signal so far at Position {best_row.iloc[0]['Position']} "
53
- f"({best_row.iloc[0]['Signal Strength (dBm)']} dBm)**")
 
 
 
 
54
 
55
- time.sleep(1) # Wait a second before the next update
 
56
 
57
- # Display final chart after stopping
58
- if st.session_state.data:
 
 
 
 
 
 
59
  df = pd.DataFrame(st.session_state.data)
60
- st.line_chart(df.set_index("Position"))
 
 
 
 
 
 
 
 
4
  import random
5
  import time
6
 
7
+ st.set_page_config(page_title="📶 WiFi Signal Analyzer", layout="centered")
8
+ st.title("📡 Real-Time WiFi Signal Strength (Bar View)")
9
 
10
+ st.markdown("Roam around and monitor your WiFi signal strength. Strong signals are shown in **green**, weak in **red**.")
11
 
12
+ # Session state for data and tracking
13
  if "tracking" not in st.session_state:
14
  st.session_state.tracking = False
15
  if "data" not in st.session_state:
16
  st.session_state.data = []
17
 
18
+ # Start / Stop buttons
19
  col1, col2 = st.columns(2)
20
  with col1:
21
  if st.button("▶️ Start Tracking", disabled=st.session_state.tracking):
 
24
  if st.button("⏹ Stop Tracking", disabled=not st.session_state.tracking):
25
  st.session_state.tracking = False
26
 
27
+ # Placeholders for real-time updates
28
+ bar_chart_placeholder = st.empty()
29
+ status_placeholder = st.empty()
30
+ best_placeholder = st.empty()
31
 
32
+ # Tracking loop
33
  while st.session_state.tracking:
34
+ signal = random.randint(-90, -30) # Simulated dBm
 
35
  position = len(st.session_state.data) + 1
36
+ st.session_state.data.append({"Position": position, "Signal": signal})
 
 
 
37
 
 
38
  df = pd.DataFrame(st.session_state.data)
39
 
40
+ # Assign colors based on signal strength
41
+ def color_by_strength(s):
42
+ if s >= -60:
43
+ return 'green'
44
+ elif s >= -75:
45
+ return 'orange'
46
+ else:
47
+ return 'red'
48
 
49
+ bar_colors = df["Signal"].apply(color_by_strength)
 
50
 
51
+ # Draw bar chart
52
+ fig, ax = plt.subplots()
53
+ ax.bar(df["Position"], df["Signal"], color=bar_colors)
54
+ ax.set_xlabel("Position")
55
+ ax.set_ylabel("Signal Strength (dBm)")
56
+ ax.set_title("WiFi Signal Strength by Position")
57
+ ax.set_ylim(-100, -20) # Consistent y-axis scale
58
+ bar_chart_placeholder.pyplot(fig)
59
 
60
+ # Status text
61
+ status_placeholder.markdown(f"📍 **Current Signal: `{signal} dBm` at Position {position}**")
62
 
63
+ # Best signal detection
64
+ best = df[df["Signal"] == df["Signal"].max()].iloc[0]
65
+ best_placeholder.markdown(f"✅ **Best Signal so far: `{best['Signal']} dBm` at Position {best['Position']}**")
66
+
67
+ time.sleep(1)
68
+
69
+ # Show final plot if tracking stopped
70
+ if not st.session_state.tracking and st.session_state.data:
71
  df = pd.DataFrame(st.session_state.data)
72
+ bar_colors = df["Signal"].apply(lambda s: 'green' if s >= -60 else 'orange' if s >= -75 else 'red')
73
+ fig, ax = plt.subplots()
74
+ ax.bar(df["Position"], df["Signal"], color=bar_colors)
75
+ ax.set_xlabel("Position")
76
+ ax.set_ylabel("Signal Strength (dBm)")
77
+ ax.set_title("Final WiFi Signal Chart")
78
+ ax.set_ylim(-100, -20)
79
+ st.pyplot(fig)