Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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,
|
| 49 |
|
| 50 |
-
await pyodide.loadPackage(["numpy", "matplotlib", "pandas", "
|
| 51 |
updateStatus('π¦ All packages loaded, setting up backends...', 'blue');
|
| 52 |
|
| 53 |
-
//
|
| 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 |
-
|
| 61 |
-
|
| 62 |
-
import
|
| 63 |
import io
|
| 64 |
import base64
|
| 65 |
-
import json
|
| 66 |
|
| 67 |
# Global variables for plot data
|
| 68 |
_current_plot_data = None
|
| 69 |
-
|
| 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
|
| 89 |
-
global
|
| 90 |
try:
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
| 93 |
except Exception as e:
|
| 94 |
-
print(f"
|
| 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 |
-
#
|
| 104 |
-
|
| 105 |
-
|
| 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
|
| 113 |
-
return
|
| 114 |
|
| 115 |
def clear_plot_data():
|
| 116 |
-
global _current_plot_data,
|
| 117 |
_current_plot_data = None
|
| 118 |
-
|
| 119 |
|
| 120 |
-
print("Pyodide ready with matplotlib AND
|
| 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
|
| 174 |
|
| 175 |
console.log('Stdout length:', stdout ? stdout.length : 0);
|
| 176 |
console.log('Matplotlib data length:', matplotlibData ? matplotlibData.length : 0);
|
| 177 |
-
console.log('
|
| 178 |
console.log('Matplotlib data preview:', matplotlibData ? matplotlibData.substring(0, 50) + '...' : 'None');
|
| 179 |
-
console.log('
|
| 180 |
|
| 181 |
// Display results
|
| 182 |
let outputDiv = document.getElementById('pyodide-output');
|
|
@@ -202,12 +201,12 @@ def create_pyodide_interface():
|
|
| 202 |
`;
|
| 203 |
}
|
| 204 |
|
| 205 |
-
if (
|
| 206 |
plotHtml += `
|
| 207 |
<div style="margin: 10px 0;">
|
| 208 |
-
<h5>π Interactive
|
| 209 |
<div style="border: 1px solid #ddd; border-radius: 3px;">
|
| 210 |
-
${
|
| 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 |
`;
|