Arrechenash commited on
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.

Files changed (1) hide show
  1. 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.setdefault(key, value)
 
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
- # UI
 
 
 
 
 
95
  if df.empty:
96
  st.info("No data found with current filters")
97
  else:
98
- tab1, tab2 = st.tabs(["๐Ÿ“Š Data", "๐Ÿ“ˆ Chart"])
99
-
100
- with tab1:
101
- st.dataframe(df, use_container_width=True)
102
-
103
- with tab2:
104
- numeric_cols = df.select_dtypes("number").columns.tolist()
105
-
106
- col1, col2 = st.columns(2)
107
- with col1:
108
- x_axis = st.selectbox(
109
- "X-axis",
110
- numeric_cols,
111
- index=(
112
- numeric_cols.index(f.get("x_axis", "open"))
113
- if f.get("x_axis", "open") in numeric_cols
114
- else 0
115
- ),
116
- key="x_axis",
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)