Spaces:
Runtime error
Runtime error
pngwn
commited on
Commit
•
f304b7c
1
Parent(s):
dfeb5f3
chaaaart
Browse files- app.py +78 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import altair as alt
|
4 |
+
from vega_datasets import data
|
5 |
+
|
6 |
+
import re
|
7 |
+
|
8 |
+
CLEAN_RE = r"<(\/*head|\/*html|\/*body|!DOCTYPE html)>"
|
9 |
+
SELECT_RE = r"document.getElementById\('vis'\)"
|
10 |
+
SELECT_RE_TWO = r"vegaEmbed\(\"#vis\", spec, embedOpt\)"
|
11 |
+
x = r".catch\(error => showError\(el, error\)\)"
|
12 |
+
|
13 |
+
|
14 |
+
def give_chart():
|
15 |
+
source = data.cars()
|
16 |
+
alt.Chart(source).mark_circle().encode(
|
17 |
+
x='Horsepower',
|
18 |
+
y='Miles_per_Gallon',
|
19 |
+
color='Origin',
|
20 |
+
).interactive().save('chart.html')
|
21 |
+
f = open("chart.html", "r").read()
|
22 |
+
f = re.sub(CLEAN_RE, "", f, flags=re.IGNORECASE)
|
23 |
+
f = re.sub(
|
24 |
+
SELECT_RE, "document.getElementsByTagName(\"gradio-app\")[0].shadowRoot.querySelectorAll(\"#vis\")[0]", f, flags=re.IGNORECASE)
|
25 |
+
f = re.sub(
|
26 |
+
SELECT_RE_TWO, "vegaEmbed(document.getElementsByTagName(\"gradio-app\")[0].shadowRoot.querySelectorAll(\"#vis\")[0], spec, embedOpt)", f, flags=re.IGNORECASE)
|
27 |
+
|
28 |
+
return f
|
29 |
+
|
30 |
+
|
31 |
+
with gr.Blocks(css=".output-html \{ display: flex;justify-content: center;\} ") as demo:
|
32 |
+
html = gr.HTML()
|
33 |
+
btn = gr.Button("Give me chart")
|
34 |
+
btn.click(give_chart, inputs=None, outputs=html,
|
35 |
+
_js="""
|
36 |
+
(x) => {
|
37 |
+
const html_wrapper = document.getElementsByTagName("gradio-app")[0].shadowRoot.querySelectorAll(".output-html")[0]
|
38 |
+
const head = document.getElementsByTagName("head")[0];
|
39 |
+
|
40 |
+
const callback = (mutationList, observer) => {
|
41 |
+
for (const mutation of mutationList) {
|
42 |
+
if (mutation.type === 'childList') {
|
43 |
+
observer.disconnect();
|
44 |
+
const scripts = Array.from(html_wrapper.querySelectorAll("script"))
|
45 |
+
.map(e => [e.type, e.src, e.textContent])
|
46 |
+
const wait = [];
|
47 |
+
const last = scripts.splice(scripts.length - 1)[0];
|
48 |
+
scripts.forEach(([type, src, inner], i, arr) => {
|
49 |
+
let r;
|
50 |
+
wait.push(new Promise((success) => r = success))
|
51 |
+
const s = document.createElement("script")
|
52 |
+
if (type) s.type = type;
|
53 |
+
if (src) s.src = src;
|
54 |
+
s.onload = () => r();
|
55 |
+
html_wrapper.appendChild(s);
|
56 |
+
s.remove();
|
57 |
+
})
|
58 |
+
|
59 |
+
Promise.all(wait).then(() => {
|
60 |
+
const s = document.createElement("script")
|
61 |
+
s.innerHTML = last[2]
|
62 |
+
html_wrapper.appendChild(s);
|
63 |
+
|
64 |
+
s.remove();
|
65 |
+
})
|
66 |
+
}
|
67 |
+
}
|
68 |
+
};
|
69 |
+
|
70 |
+
const observer = new MutationObserver(callback);
|
71 |
+
observer.observe(
|
72 |
+
html_wrapper, { attributes: false, childList: true, subtree: true }
|
73 |
+
);
|
74 |
+
|
75 |
+
}
|
76 |
+
""")
|
77 |
+
|
78 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
altair
|
2 |
+
vega_datasets
|