Update src/streamlit_app.py

#1
Files changed (1) hide show
  1. src/streamlit_app.py +19 -0
src/streamlit_app.py CHANGED
@@ -85,12 +85,28 @@ def get_image_base64(fig):
85
  fig.savefig(buf, format="png", bbox_inches='tight')
86
  return base64.b64encode(buf.getvalue()).decode('utf-8')
87
 
 
 
 
 
 
 
 
 
88
  def ai_explainer_ui(fig, key_id):
89
  state_key = f"explanation_{key_id}"
90
 
91
  if state_key not in st.session_state:
92
  st.session_state[state_key] = None
93
 
 
 
 
 
 
 
 
 
94
  if st.button("Explain with AI", key=f"btn_{key_id}"):
95
  # Use the key we retrieved at the top of the script
96
  api_key = GROQ_API_KEY
@@ -116,6 +132,9 @@ def ai_explainer_ui(fig, key_id):
116
 
117
  if response.status_code == 200:
118
  st.session_state[state_key] = response.json()['explanation']
 
 
 
119
  else:
120
  st.error(f"API Error: {response.status_code}")
121
  except Exception as e:
 
85
  fig.savefig(buf, format="png", bbox_inches='tight')
86
  return base64.b64encode(buf.getvalue()).decode('utf-8')
87
 
88
+
89
+ @st.fragment
90
+ def render_chart_section(title, fig, key_id):
91
+ with st.container(border=True):
92
+ st.subheader(title)
93
+ st.pyplot(fig)
94
+ ai_explainer_ui(fig, key_id)
95
+
96
  def ai_explainer_ui(fig, key_id):
97
  state_key = f"explanation_{key_id}"
98
 
99
  if state_key not in st.session_state:
100
  st.session_state[state_key] = None
101
 
102
+
103
+ # Create a reserved slot
104
+ info_slot = st.empty()
105
+
106
+ # Show existing explanation immediately in the reserved slot
107
+ if st.session_state[state_key]:
108
+ info_slot.info(st.session_state[state_key])
109
+
110
  if st.button("Explain with AI", key=f"btn_{key_id}"):
111
  # Use the key we retrieved at the top of the script
112
  api_key = GROQ_API_KEY
 
132
 
133
  if response.status_code == 200:
134
  st.session_state[state_key] = response.json()['explanation']
135
+
136
+ st.session_state[state_key] = explanation_text
137
+ info_slot.info(explanation_text)
138
  else:
139
  st.error(f"API Error: {response.status_code}")
140
  except Exception as e: