Update app.py
Browse files
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("
|
| 9 |
|
| 10 |
-
st.
|
| 11 |
|
| 12 |
-
#
|
| 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
|
| 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 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
|
|
|
| 32 |
while st.session_state.tracking:
|
| 33 |
-
|
| 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 |
-
#
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
graph_placeholder.line_chart(df.set_index("Position"))
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
df = pd.DataFrame(st.session_state.data)
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|