Spaces:
Sleeping
Sleeping
File size: 4,366 Bytes
d1ae699 b4d9d47 d1ae699 81b031b b4d9d47 81b031b dcae7f9 735010b 81b031b b4d9d47 944bc86 b4d9d47 9b66796 944bc86 b4d9d47 4b10064 d1ae699 f7d9f2d b4d9d47 0f968e0 b4d9d47 a791ab7 b4d9d47 81b031b d1ae699 b4d9d47 f7d9f2d 944bc86 b4d9d47 a791ab7 b4d9d47 e45c34b b4d9d47 4b10064 f7d9f2d 735010b b4d9d47 944bc86 b4d9d47 81b031b dcae7f9 b4d9d47 dcae7f9 0f968e0 b4d9d47 f7d9f2d b4d9d47 f7d9f2d 0f968e0 b4d9d47 0f968e0 b4d9d47 0f968e0 b4d9d47 0f968e0 b4d9d47 0f968e0 b4d9d47 0f968e0 b4d9d47 a787b7c b4d9d47 a787b7c b4d9d47 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | #!/usr/bin/env python3
"""
Minimal Working Plotly - Simple and Clean
"""
import gradio as gr
def create_interface():
html = '''
<div style="border: 1px solid #ddd; padding: 20px; border-radius: 5px;">
<div id="status">๐ Loading...</div>
<div id="output" style="display:none; margin-top: 20px;">
<pre id="text" style="background: #f5f5f5; padding: 10px; max-height: 150px; overflow-y: auto;"></pre>
<div id="plots"></div>
</div>
</div>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script src="https://cdn.jsdelivr.net/pyodide/v0.25.0/full/pyodide.js"></script>
<script>
let pyodide, ready = false, plotNum = 0;
function status(msg, color = 'black') {
document.getElementById('status').innerHTML = msg;
document.getElementById('status').style.color = color;
}
window.makePlot = function(data, layout) {
const id = 'plot' + (++plotNum);
const div = document.createElement('div');
div.innerHTML = `<h4>Plot ${plotNum}</h4><div id="${id}" style="width:100%; height:400px; border:1px solid #ccc;"></div>`;
document.getElementById('plots').appendChild(div);
Plotly.newPlot(id, JSON.parse(data), JSON.parse(layout));
return true;
};
async function init() {
status('Loading...', 'blue');
pyodide = await loadPyodide();
await pyodide.loadPackage(['numpy', 'pandas', 'micropip']);
await pyodide.runPythonAsync('import micropip; await micropip.install("plotly")');
pyodide.runPython(`
import json, numpy as np
from js import makePlot
def show(fig):
d = fig.to_dict()
# Convert numpy arrays to lists
for trace in d.get('data', []):
if 'x' in trace and hasattr(trace['x'], 'tolist'):
trace['x'] = trace['x'].tolist()
if 'y' in trace and hasattr(trace['y'], 'tolist'):
trace['y'] = trace['y'].tolist()
makePlot(json.dumps(d['data']), json.dumps(d.get('layout', {})))
print("Plot created!")
import plotly.graph_objects as go
import plotly.express as px
go.Figure.show = lambda self: show(self)
`);
ready = true;
status('โ
Ready!', 'green');
document.getElementById('output').style.display = 'block';
document.getElementById('text').textContent = 'Ready to plot!';
}
async function run(code) {
if (!ready) return 'Not ready';
document.getElementById('plots').innerHTML = '';
pyodide.runPython(`
import sys
from io import StringIO
old = sys.stdout
sys.stdout = cap = StringIO()
`);
pyodide.runPython(code);
const output = pyodide.runPython('sys.stdout = old; cap.getvalue()');
document.getElementById('text').textContent = output || 'Done';
return output || 'Success';
}
// Start
setTimeout(() => {
if (typeof loadPyodide !== 'undefined' && typeof Plotly !== 'undefined') {
init();
}
}, 1000);
window.run = run;
</script>
'''
return html
with gr.Blocks() as demo:
gr.Markdown("# ๐ Simple Plotly")
interface = gr.HTML(create_interface())
with gr.Row():
code = gr.Textbox(
value="""# Test 1: Graph Objects
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(x=[1,2,3,4], y=[1,4,9,16], mode='markers+lines'))
fig.show()
# Test 2: Express
import plotly.express as px
import pandas as pd
df = pd.DataFrame({'x': [1,2,3,4], 'y': [1,4,9,16]})
fig = px.scatter(df, x='x', y='y')
fig.show()""",
lines=10,
label="Code"
)
result = gr.Textbox(label="Result", lines=2, interactive=False)
btn = gr.Button("โถ๏ธ Run")
btn.click(
fn=None,
inputs=[code],
outputs=[result],
js="(code) => window.run ? window.run(code) : 'Loading...'"
)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860) |