Spaces:
Sleeping
Sleeping
Commit
ยท
d6858c0
1
Parent(s):
7fbdb01
Refactor UI and improve state management
Browse files- Refactored the UI to remove tabs and display the chart and data on the main page.
- Improved session state initialization to prevent overwriting widget values on script rerun.
- Added a results count to the sidebar to show the number of data points.
- Simplified chart axis selection by using session state directly.
- src/streamlit_app.py +32 -38
src/streamlit_app.py
CHANGED
|
@@ -33,7 +33,7 @@ def load_symbols():
|
|
| 33 |
)
|
| 34 |
|
| 35 |
|
| 36 |
-
# Initialize session state
|
| 37 |
defaults = {
|
| 38 |
"date": None,
|
| 39 |
"symbols": [],
|
|
@@ -46,7 +46,8 @@ defaults = {
|
|
| 46 |
"y_axis": "change_pct",
|
| 47 |
}
|
| 48 |
for key, value in defaults.items():
|
| 49 |
-
st.session_state
|
|
|
|
| 50 |
|
| 51 |
# Sidebar filters
|
| 52 |
with st.sidebar:
|
|
@@ -70,6 +71,9 @@ with st.sidebar:
|
|
| 70 |
"Min rel volume", value=st.session_state.min_relvol
|
| 71 |
)
|
| 72 |
|
|
|
|
|
|
|
|
|
|
| 73 |
# Construct query filters
|
| 74 |
f = st.session_state
|
| 75 |
filters = []
|
|
@@ -91,42 +95,32 @@ if f.min_run:
|
|
| 91 |
# Load data
|
| 92 |
df = get_data(filters if filters else None)
|
| 93 |
|
| 94 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
if df.empty:
|
| 96 |
st.info("No data found with current filters")
|
| 97 |
else:
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
with col2:
|
| 119 |
-
y_axis = st.selectbox(
|
| 120 |
-
"Y-axis",
|
| 121 |
-
numeric_cols,
|
| 122 |
-
index=(
|
| 123 |
-
numeric_cols.index(f.get("y_axis", "change_pct"))
|
| 124 |
-
if f.get("y_axis", "change_pct") in numeric_cols
|
| 125 |
-
else 1
|
| 126 |
-
),
|
| 127 |
-
key="y_axis",
|
| 128 |
-
)
|
| 129 |
-
|
| 130 |
-
st.scatter_chart(df, x=x_axis, y=y_axis, color="symbol")
|
| 131 |
-
|
| 132 |
-
st.write(f"Results: {len(df)}")
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
|
| 36 |
+
# Initialize session state without forcing values onto widgets
|
| 37 |
defaults = {
|
| 38 |
"date": None,
|
| 39 |
"symbols": [],
|
|
|
|
| 46 |
"y_axis": "change_pct",
|
| 47 |
}
|
| 48 |
for key, value in defaults.items():
|
| 49 |
+
if key not in st.session_state:
|
| 50 |
+
st.session_state[key] = value
|
| 51 |
|
| 52 |
# Sidebar filters
|
| 53 |
with st.sidebar:
|
|
|
|
| 71 |
"Min rel volume", value=st.session_state.min_relvol
|
| 72 |
)
|
| 73 |
|
| 74 |
+
# Placeholder for results count (updated after df load)
|
| 75 |
+
results_placeholder = st.empty()
|
| 76 |
+
|
| 77 |
# Construct query filters
|
| 78 |
f = st.session_state
|
| 79 |
filters = []
|
|
|
|
| 95 |
# Load data
|
| 96 |
df = get_data(filters if filters else None)
|
| 97 |
|
| 98 |
+
# Update the sidebar with results count
|
| 99 |
+
with results_placeholder:
|
| 100 |
+
st.markdown("---")
|
| 101 |
+
st.markdown(f"**Results: {len(df)}**")
|
| 102 |
+
|
| 103 |
+
# UI rendering
|
| 104 |
if df.empty:
|
| 105 |
st.info("No data found with current filters")
|
| 106 |
else:
|
| 107 |
+
numeric_cols = df.select_dtypes("number").columns.tolist()
|
| 108 |
+
|
| 109 |
+
col1, col2 = st.columns(2)
|
| 110 |
+
with col1:
|
| 111 |
+
st.selectbox(
|
| 112 |
+
"X-axis",
|
| 113 |
+
numeric_cols,
|
| 114 |
+
key="x_axis",
|
| 115 |
+
)
|
| 116 |
+
with col2:
|
| 117 |
+
st.selectbox(
|
| 118 |
+
"Y-axis",
|
| 119 |
+
numeric_cols,
|
| 120 |
+
key="y_axis", # โ
No index or value conflicts
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
st.scatter_chart(
|
| 124 |
+
df, x=st.session_state.x_axis, y=st.session_state.y_axis, color="symbol"
|
| 125 |
+
)
|
| 126 |
+
st.dataframe(df, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|