mic3333 commited on
Commit
e3e94e7
Β·
verified Β·
1 Parent(s): 9f45a5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -29
app.py CHANGED
@@ -45,28 +45,27 @@ def create_pyodide_interface():
45
  indexURL: "https://cdn.jsdelivr.net/pyodide/v0.24.1/full/"
46
  });
47
 
48
- updateStatus('πŸ“¦ Loading Python packages (numpy, matplotlib, pandas, plotly)...', 'blue');
49
 
50
- await pyodide.loadPackage(["numpy", "matplotlib", "pandas", "plotly"]);
51
  updateStatus('πŸ“¦ All packages loaded, setting up backends...', 'blue');
52
 
53
- // Set up matplotlib backend for web - IMPROVED VERSION
54
  pyodide.runPython(`
55
  import matplotlib
56
  matplotlib.use('AGG')
57
  import matplotlib.pyplot as plt
58
  import numpy as np
59
  import pandas as pd
60
- import plotly.graph_objects as go
61
- import plotly.express as px
62
- import plotly
63
  import io
64
  import base64
65
- import json
66
 
67
  # Global variables for plot data
68
  _current_plot_data = None
69
- _current_plotly_data = None
70
 
71
  def capture_matplotlib():
72
  global _current_plot_data
@@ -85,13 +84,15 @@ def create_pyodide_interface():
85
  print(f"Matplotlib capture error: {e}")
86
  return None
87
 
88
- def capture_plotly(fig):
89
- global _current_plotly_data
90
  try:
91
- _current_plotly_data = fig.to_html(include_plotlyjs='cdn', div_id='plotly-div')
92
- return _current_plotly_data
 
 
93
  except Exception as e:
94
- print(f"Plotly capture error: {e}")
95
  return None
96
 
97
  # Override functions
@@ -100,24 +101,22 @@ def create_pyodide_interface():
100
  return capture_matplotlib()
101
  plt.show = custom_show
102
 
103
- # Override plotly show
104
- original_plotly_show = go.Figure.show
105
- def custom_plotly_show(self, *args, **kwargs):
106
- return capture_plotly(self)
107
- go.Figure.show = custom_plotly_show
108
 
109
  def get_matplotlib_data():
110
  return _current_plot_data
111
 
112
- def get_plotly_data():
113
- return _current_plotly_data
114
 
115
  def clear_plot_data():
116
- global _current_plot_data, _current_plotly_data
117
  _current_plot_data = None
118
- _current_plotly_data = None
119
 
120
- print("Pyodide ready with matplotlib AND plotly support!")
121
  `);
122
 
123
  pyodideReady = true;
@@ -170,13 +169,13 @@ def create_pyodide_interface():
170
 
171
  // Get both types of plot data
172
  let matplotlibData = pyodide.runPython('get_matplotlib_data()');
173
- let plotlyData = pyodide.runPython('get_plotly_data()');
174
 
175
  console.log('Stdout length:', stdout ? stdout.length : 0);
176
  console.log('Matplotlib data length:', matplotlibData ? matplotlibData.length : 0);
177
- console.log('Plotly data length:', plotlyData ? plotlyData.length : 0);
178
  console.log('Matplotlib data preview:', matplotlibData ? matplotlibData.substring(0, 50) + '...' : 'None');
179
- console.log('Plotly data preview:', plotlyData ? plotlyData.substring(0, 50) + '...' : 'None');
180
 
181
  // Display results
182
  let outputDiv = document.getElementById('pyodide-output');
@@ -202,12 +201,12 @@ def create_pyodide_interface():
202
  `;
203
  }
204
 
205
- if (plotlyData) {
206
  plotHtml += `
207
  <div style="margin: 10px 0;">
208
- <h5>πŸ“Š Interactive Plotly Plot:</h5>
209
  <div style="border: 1px solid #ddd; border-radius: 3px;">
210
- ${plotlyData}
211
  </div>
212
  </div>
213
  `;
 
45
  indexURL: "https://cdn.jsdelivr.net/pyodide/v0.24.1/full/"
46
  });
47
 
48
+ updateStatus('πŸ“¦ Loading Python packages (numpy, matplotlib, pandas, bokeh)...', 'blue');
49
 
50
+ await pyodide.loadPackage(["numpy", "matplotlib", "pandas", "bokeh"]);
51
  updateStatus('πŸ“¦ All packages loaded, setting up backends...', 'blue');
52
 
53
+ // Update the Python setup:
54
  pyodide.runPython(`
55
  import matplotlib
56
  matplotlib.use('AGG')
57
  import matplotlib.pyplot as plt
58
  import numpy as np
59
  import pandas as pd
60
+ from bokeh.plotting import figure, show, output_file
61
+ from bokeh.models import HoverTool
62
+ from bokeh.io import export_png
63
  import io
64
  import base64
 
65
 
66
  # Global variables for plot data
67
  _current_plot_data = None
68
+ _current_bokeh_html = None
69
 
70
  def capture_matplotlib():
71
  global _current_plot_data
 
84
  print(f"Matplotlib capture error: {e}")
85
  return None
86
 
87
+ def capture_bokeh(bokeh_fig):
88
+ global _current_bokeh_html
89
  try:
90
+ from bokeh.embed import file_html
91
+ from bokeh.resources import CDN
92
+ _current_bokeh_html = file_html(bokeh_fig, CDN, "Bokeh Plot")
93
+ return _current_bokeh_html
94
  except Exception as e:
95
+ print(f"Bokeh capture error: {e}")
96
  return None
97
 
98
  # Override functions
 
101
  return capture_matplotlib()
102
  plt.show = custom_show
103
 
104
+ # Custom bokeh show function
105
+ def show_bokeh(fig):
106
+ return capture_bokeh(fig)
 
 
107
 
108
  def get_matplotlib_data():
109
  return _current_plot_data
110
 
111
+ def get_bokeh_data():
112
+ return _current_bokeh_html
113
 
114
  def clear_plot_data():
115
+ global _current_plot_data, _current_bokeh_html
116
  _current_plot_data = None
117
+ _current_bokeh_html = None
118
 
119
+ print("Pyodide ready with matplotlib AND bokeh support!")
120
  `);
121
 
122
  pyodideReady = true;
 
169
 
170
  // Get both types of plot data
171
  let matplotlibData = pyodide.runPython('get_matplotlib_data()');
172
+ let bokehData = pyodide.runPython('get_bokeh_data()');
173
 
174
  console.log('Stdout length:', stdout ? stdout.length : 0);
175
  console.log('Matplotlib data length:', matplotlibData ? matplotlibData.length : 0);
176
+ console.log('Bokeh data length:', bokehData ? bokehData.length : 0);
177
  console.log('Matplotlib data preview:', matplotlibData ? matplotlibData.substring(0, 50) + '...' : 'None');
178
+ console.log('Bokeh data preview:', bokehData ? bokehData.substring(0, 50) + '...' : 'None');
179
 
180
  // Display results
181
  let outputDiv = document.getElementById('pyodide-output');
 
201
  `;
202
  }
203
 
204
+ if (bokehData) {
205
  plotHtml += `
206
  <div style="margin: 10px 0;">
207
+ <h5>πŸ“Š Interactive Bokeh Plot:</h5>
208
  <div style="border: 1px solid #ddd; border-radius: 3px;">
209
+ ${bokehData}
210
  </div>
211
  </div>
212
  `;