Nipun Claude commited on
Commit
ca71337
Β·
1 Parent(s): a60f4e3

Implement direct matplotlib figure display with st.pyplot()

Browse files

- Added streamlit import to code execution environment
- Updated plotting requirements to use st.pyplot(fig) for direct display
- Should provide better resolution and faster rendering than file-based approach
- Fixed immediate user message display without rerun delays

πŸ€– Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (3) hide show
  1. app.py +4 -1
  2. new_system_prompt.txt +3 -4
  3. src.py +2 -1
app.py CHANGED
@@ -494,7 +494,10 @@ def show_custom_response(response):
494
  with st.expander("πŸ“‹ View Generated Code", expanded=False):
495
  st.code(response["gen_code"], language="python")
496
 
497
- # Try to display image if content is a file path
 
 
 
498
  try:
499
  if isinstance(content, str) and (content.endswith('.png') or content.endswith('.jpg')):
500
  if os.path.exists(content):
 
494
  with st.expander("πŸ“‹ View Generated Code", expanded=False):
495
  st.code(response["gen_code"], language="python")
496
 
497
+ # Check if this is a plot response (plots are now displayed directly via st.pyplot)
498
+ is_plot_response = isinstance(content, str) and "Plot displayed successfully" in content
499
+
500
+ # Try to display image if content is a file path (for backward compatibility)
501
  try:
502
  if isinstance(content, str) and (content.endswith('.png') or content.endswith('.jpg')):
503
  if os.path.exists(content):
new_system_prompt.txt CHANGED
@@ -33,10 +33,9 @@ DATA SAFETY:
33
  - Use .dropna() to remove missing values before analysis
34
 
35
  PLOTTING REQUIREMENTS:
36
- - Create plots for visualization requests: plt.figure(figsize=(12, 7))
37
- - Save plots with ultra-high resolution: filename = f"plot_{uuid.uuid4().hex[:8]}.png"; plt.savefig(filename, dpi=1200, bbox_inches='tight', facecolor='white', edgecolor='none')
38
- - Close plots: plt.close()
39
- - Store filename: answer = filename
40
  - For non-plots: answer = "text result"
41
 
42
  BASIC ERROR PREVENTION:
 
33
  - Use .dropna() to remove missing values before analysis
34
 
35
  PLOTTING REQUIREMENTS:
36
+ - Create plots for visualization requests: fig, ax = plt.subplots(figsize=(12, 7))
37
+ - Display plots directly: st.pyplot(fig); plt.close()
38
+ - Store success message: answer = "Plot displayed successfully"
 
39
  - For non-plots: answer = "text result"
40
 
41
  BASIC ERROR PREVENTION:
src.py CHANGED
@@ -166,7 +166,8 @@ def ask_question(model_name, question):
166
  'uuid': __import__('uuid'),
167
  'calendar': __import__('calendar'),
168
  'np': __import__('numpy'),
169
- 'df': df # <-- pass your DataFrame here
 
170
  }
171
 
172
  # allow user to inject more globals (optional)
 
166
  'uuid': __import__('uuid'),
167
  'calendar': __import__('calendar'),
168
  'np': __import__('numpy'),
169
+ 'df': df, # <-- pass your DataFrame here
170
+ 'st': __import__('streamlit') # Add streamlit for st.pyplot()
171
  }
172
 
173
  # allow user to inject more globals (optional)