Update gradio-lite templates to allow for hot-reload.
Browse files
app.py
CHANGED
|
@@ -116,27 +116,27 @@ with gr.Blocks() as demo:
|
|
| 116 |
gr.Markdown("<h1 id=\"TEST\" align=\"center\">KiteWind πͺπ</h1>")
|
| 117 |
gr.Markdown(
|
| 118 |
"<h4 align=\"center\">Chat-assisted web app creator by <a href=\"https://huggingface.co/gstaff\">@gstaff</a></h4>")
|
| 119 |
-
selectedTab = gr.State(value='
|
| 120 |
-
with gr.Tab('
|
| 121 |
with gr.Row():
|
| 122 |
with gr.Column():
|
| 123 |
gr.Markdown("## 1. Run your app in the browser!")
|
| 124 |
-
gr.HTML(value='<div id="
|
| 125 |
gr.Markdown("## 2. Customize using voice requests!")
|
| 126 |
with gr.Row():
|
| 127 |
with gr.Column():
|
| 128 |
with gr.Group():
|
| 129 |
in_audio = gr.Audio(label="Record a voice request (click or press ctrl + ` to start/stop)", source='microphone', type='filepath')
|
| 130 |
in_prompt = gr.Textbox(label="Or type a text request and press Enter",
|
| 131 |
-
placeholder="Need an idea? Try one of these:\n- Add a button to reverse the name\n- Change the greeting to Spanish\n-
|
| 132 |
out_text = gr.TextArea(label="Chat Assistant Response")
|
| 133 |
-
|
| 134 |
with gr.Column():
|
| 135 |
code_area = gr.Code(label="App Code - You can also edit directly and then click Update App",
|
| 136 |
-
language='python', value=starting_app_code(DemoType.
|
| 137 |
update_btn = gr.Button("Update App", variant="primary")
|
| 138 |
code_update_params = {'fn': None, 'inputs': code_area, 'outputs': None,
|
| 139 |
-
'_js': update_iframe_js(DemoType.
|
| 140 |
gen_text_params = {'fn': generate_text, 'inputs': [code_area, in_prompt],
|
| 141 |
'outputs': [out_text, code_area]}
|
| 142 |
transcribe_params = {'fn': transcribe, 'inputs': [in_audio], 'outputs': [in_prompt, in_audio]}
|
|
@@ -147,35 +147,35 @@ with gr.Blocks() as demo:
|
|
| 147 |
with gr.Column():
|
| 148 |
gr.Markdown("## 3. Export your app to share!")
|
| 149 |
copy_snippet_btn = gr.Button("Copy app snippet to paste in another page")
|
| 150 |
-
copy_snippet_btn.click(copy_notify, code_area, None, _js=copy_snippet_js(DemoType.
|
| 151 |
download_btn = gr.Button("Download app as a standalone file")
|
| 152 |
-
download_btn.click(None, code_area, None, _js=download_code_js(DemoType.
|
| 153 |
with gr.Row():
|
| 154 |
with gr.Column():
|
| 155 |
gr.Markdown("## Current limitations")
|
| 156 |
with gr.Accordion("Click to view", open=False):
|
| 157 |
gr.Markdown(
|
| 158 |
-
"- Only
|
| 159 |
-
with gr.Tab('
|
| 160 |
with gr.Row():
|
| 161 |
with gr.Column():
|
| 162 |
gr.Markdown("## 1. Run your app in the browser!")
|
| 163 |
-
gr.HTML(value='<div id="
|
| 164 |
gr.Markdown("## 2. Customize using voice requests!")
|
| 165 |
with gr.Row():
|
| 166 |
with gr.Column():
|
| 167 |
with gr.Group():
|
| 168 |
in_audio = gr.Audio(label="Record a voice request (click or press ctrl + ` to start/stop)", source='microphone', type='filepath')
|
| 169 |
in_prompt = gr.Textbox(label="Or type a text request and press Enter",
|
| 170 |
-
placeholder="Need an idea? Try one of these:\n- Add a button to reverse the name\n- Change the greeting to
|
| 171 |
out_text = gr.TextArea(label="Chat Assistant Response")
|
| 172 |
-
|
| 173 |
with gr.Column():
|
| 174 |
code_area = gr.Code(label="App Code - You can also edit directly and then click Update App",
|
| 175 |
-
language='python', value=starting_app_code(DemoType.
|
| 176 |
update_btn = gr.Button("Update App", variant="primary")
|
| 177 |
code_update_params = {'fn': None, 'inputs': code_area, 'outputs': None,
|
| 178 |
-
'_js': update_iframe_js(DemoType.
|
| 179 |
gen_text_params = {'fn': generate_text, 'inputs': [code_area, in_prompt],
|
| 180 |
'outputs': [out_text, code_area]}
|
| 181 |
transcribe_params = {'fn': transcribe, 'inputs': [in_audio], 'outputs': [in_prompt, in_audio]}
|
|
@@ -186,20 +186,19 @@ with gr.Blocks() as demo:
|
|
| 186 |
with gr.Column():
|
| 187 |
gr.Markdown("## 3. Export your app to share!")
|
| 188 |
copy_snippet_btn = gr.Button("Copy app snippet to paste in another page")
|
| 189 |
-
copy_snippet_btn.click(copy_notify, code_area, None, _js=copy_snippet_js(DemoType.
|
| 190 |
download_btn = gr.Button("Download app as a standalone file")
|
| 191 |
-
download_btn.click(None, code_area, None, _js=download_code_js(DemoType.
|
| 192 |
with gr.Row():
|
| 193 |
with gr.Column():
|
| 194 |
gr.Markdown("## Current limitations")
|
| 195 |
with gr.Accordion("Click to view", open=False):
|
| 196 |
gr.Markdown(
|
| 197 |
-
"- Only
|
| 198 |
-
|
| 199 |
-
stlite_tab.select(lambda: "stlite", None, selectedTab).then(None, None, None, _js=load_js(DemoType.STREAMLIT))
|
| 200 |
gradio_lite_tab.select(lambda: "gradio-lite", None, selectedTab).then(None, None, None,
|
| 201 |
_js=load_js(DemoType.GRADIO))
|
| 202 |
-
|
|
|
|
| 203 |
demo.load(None, None, None, _js=add_hotkeys())
|
| 204 |
demo.css = "footer {visibility: hidden}"
|
| 205 |
|
|
|
|
| 116 |
gr.Markdown("<h1 id=\"TEST\" align=\"center\">KiteWind πͺπ</h1>")
|
| 117 |
gr.Markdown(
|
| 118 |
"<h4 align=\"center\">Chat-assisted web app creator by <a href=\"https://huggingface.co/gstaff\">@gstaff</a></h4>")
|
| 119 |
+
selectedTab = gr.State(value='gradio-lite')
|
| 120 |
+
with gr.Tab('Gradio (gradio-lite)') as gradio_lite_tab:
|
| 121 |
with gr.Row():
|
| 122 |
with gr.Column():
|
| 123 |
gr.Markdown("## 1. Run your app in the browser!")
|
| 124 |
+
gr.HTML(value='<div id="gradioDemoDiv"></div>')
|
| 125 |
gr.Markdown("## 2. Customize using voice requests!")
|
| 126 |
with gr.Row():
|
| 127 |
with gr.Column():
|
| 128 |
with gr.Group():
|
| 129 |
in_audio = gr.Audio(label="Record a voice request (click or press ctrl + ` to start/stop)", source='microphone', type='filepath')
|
| 130 |
in_prompt = gr.Textbox(label="Or type a text request and press Enter",
|
| 131 |
+
placeholder="Need an idea? Try one of these:\n- Add a button to reverse the name\n- Change the greeting to Spanish\n- Put the reversed name output into a separate textbox")
|
| 132 |
out_text = gr.TextArea(label="Chat Assistant Response")
|
| 133 |
+
clear = gr.ClearButton([in_prompt, in_audio, out_text])
|
| 134 |
with gr.Column():
|
| 135 |
code_area = gr.Code(label="App Code - You can also edit directly and then click Update App",
|
| 136 |
+
language='python', value=starting_app_code(DemoType.GRADIO))
|
| 137 |
update_btn = gr.Button("Update App", variant="primary")
|
| 138 |
code_update_params = {'fn': None, 'inputs': code_area, 'outputs': None,
|
| 139 |
+
'_js': update_iframe_js(DemoType.GRADIO)}
|
| 140 |
gen_text_params = {'fn': generate_text, 'inputs': [code_area, in_prompt],
|
| 141 |
'outputs': [out_text, code_area]}
|
| 142 |
transcribe_params = {'fn': transcribe, 'inputs': [in_audio], 'outputs': [in_prompt, in_audio]}
|
|
|
|
| 147 |
with gr.Column():
|
| 148 |
gr.Markdown("## 3. Export your app to share!")
|
| 149 |
copy_snippet_btn = gr.Button("Copy app snippet to paste in another page")
|
| 150 |
+
copy_snippet_btn.click(copy_notify, code_area, None, _js=copy_snippet_js(DemoType.GRADIO))
|
| 151 |
download_btn = gr.Button("Download app as a standalone file")
|
| 152 |
+
download_btn.click(None, code_area, None, _js=download_code_js(DemoType.GRADIO))
|
| 153 |
with gr.Row():
|
| 154 |
with gr.Column():
|
| 155 |
gr.Markdown("## Current limitations")
|
| 156 |
with gr.Accordion("Click to view", open=False):
|
| 157 |
gr.Markdown(
|
| 158 |
+
"- Only gradio-lite apps using the python standard libraries and gradio are supported\n- The chat hasn't been tuned on gradio library data; it may make mistakes\n- The app needs to fully reload each time it is changed")
|
| 159 |
+
with gr.Tab('Streamlit (stlite)') as stlite_tab:
|
| 160 |
with gr.Row():
|
| 161 |
with gr.Column():
|
| 162 |
gr.Markdown("## 1. Run your app in the browser!")
|
| 163 |
+
gr.HTML(value='<div id="stliteDemoDiv"></div>')
|
| 164 |
gr.Markdown("## 2. Customize using voice requests!")
|
| 165 |
with gr.Row():
|
| 166 |
with gr.Column():
|
| 167 |
with gr.Group():
|
| 168 |
in_audio = gr.Audio(label="Record a voice request (click or press ctrl + ` to start/stop)", source='microphone', type='filepath')
|
| 169 |
in_prompt = gr.Textbox(label="Or type a text request and press Enter",
|
| 170 |
+
placeholder="Need an idea? Try one of these:\n- Add a button to reverse the name\n- Change the greeting to Spanish\n- Make the button primary")
|
| 171 |
out_text = gr.TextArea(label="Chat Assistant Response")
|
| 172 |
+
clear_btn = gr.ClearButton([in_prompt, in_audio, out_text])
|
| 173 |
with gr.Column():
|
| 174 |
code_area = gr.Code(label="App Code - You can also edit directly and then click Update App",
|
| 175 |
+
language='python', value=starting_app_code(DemoType.STREAMLIT))
|
| 176 |
update_btn = gr.Button("Update App", variant="primary")
|
| 177 |
code_update_params = {'fn': None, 'inputs': code_area, 'outputs': None,
|
| 178 |
+
'_js': update_iframe_js(DemoType.STREAMLIT)}
|
| 179 |
gen_text_params = {'fn': generate_text, 'inputs': [code_area, in_prompt],
|
| 180 |
'outputs': [out_text, code_area]}
|
| 181 |
transcribe_params = {'fn': transcribe, 'inputs': [in_audio], 'outputs': [in_prompt, in_audio]}
|
|
|
|
| 186 |
with gr.Column():
|
| 187 |
gr.Markdown("## 3. Export your app to share!")
|
| 188 |
copy_snippet_btn = gr.Button("Copy app snippet to paste in another page")
|
| 189 |
+
copy_snippet_btn.click(copy_notify, code_area, None, _js=copy_snippet_js(DemoType.STREAMLIT))
|
| 190 |
download_btn = gr.Button("Download app as a standalone file")
|
| 191 |
+
download_btn.click(None, code_area, None, _js=download_code_js(DemoType.STREAMLIT))
|
| 192 |
with gr.Row():
|
| 193 |
with gr.Column():
|
| 194 |
gr.Markdown("## Current limitations")
|
| 195 |
with gr.Accordion("Click to view", open=False):
|
| 196 |
gr.Markdown(
|
| 197 |
+
"- Only Streamlit apps using libraries available in pyodide are supported\n- The chat hasn't been tuned on Streamlit library data; it may make mistakes")
|
|
|
|
|
|
|
| 198 |
gradio_lite_tab.select(lambda: "gradio-lite", None, selectedTab).then(None, None, None,
|
| 199 |
_js=load_js(DemoType.GRADIO))
|
| 200 |
+
stlite_tab.select(lambda: "stlite", None, selectedTab).then(None, None, None, _js=load_js(DemoType.STREAMLIT))
|
| 201 |
+
demo.load(None, None, None, _js=load_js(DemoType.GRADIO))
|
| 202 |
demo.load(None, None, None, _js=add_hotkeys())
|
| 203 |
demo.css = "footer {visibility: hidden}"
|
| 204 |
|
templates.py
CHANGED
|
@@ -9,12 +9,13 @@ class DemoType(Enum):
|
|
| 9 |
|
| 10 |
gradio_lite_html_template = Path('templates/gradio-lite/gradio-lite-template.html').read_text()
|
| 11 |
stlite_html_template = Path('templates/stlite/stlite-template.html').read_text()
|
|
|
|
| 12 |
stlite_snippet_template = Path('templates/stlite/stlite-snippet-template.html').read_text()
|
| 13 |
|
| 14 |
|
| 15 |
def starting_app_code(demo_type: DemoType) -> str:
|
| 16 |
if demo_type == DemoType.GRADIO:
|
| 17 |
-
return Path('templates/gradio-lite/gradio_lite_starting_code.py').read_text()
|
| 18 |
elif demo_type == DemoType.STREAMLIT:
|
| 19 |
return Path('templates/stlite/stlite_starting_code.py').read_text().replace('`', r'\`')
|
| 20 |
raise NotImplementedError(f'{demo_type} is not a supported demo type')
|
|
@@ -26,16 +27,17 @@ def load_js(demo_type: DemoType) -> str:
|
|
| 26 |
if (window.gradioLiteLoaded) {{
|
| 27 |
return
|
| 28 |
}}
|
| 29 |
-
const htmlString = '<iframe
|
| 30 |
const parser = new DOMParser();
|
| 31 |
const doc = parser.parseFromString(htmlString, 'text/html');
|
| 32 |
-
const iframe = doc.
|
| 33 |
const div = document.getElementById('gradioDemoDiv');
|
| 34 |
div.appendChild(iframe);
|
| 35 |
|
| 36 |
-
const
|
|
|
|
| 37 |
frame.contentWindow.document.open('text/html', 'replace');
|
| 38 |
-
frame.contentWindow.document.write(
|
| 39 |
frame.contentWindow.document.close();
|
| 40 |
window.gradioLiteLoaded = true;
|
| 41 |
}}"""
|
|
@@ -44,15 +46,15 @@ def load_js(demo_type: DemoType) -> str:
|
|
| 44 |
if (window.stliteLoaded) {{
|
| 45 |
return
|
| 46 |
}}
|
| 47 |
-
const htmlString = '<iframe id="
|
| 48 |
const parser = new DOMParser();
|
| 49 |
const doc = parser.parseFromString(htmlString, 'text/html');
|
| 50 |
-
const iframe = doc.getElementById('
|
| 51 |
const div = document.getElementById('stliteDemoDiv');
|
| 52 |
div.appendChild(iframe);
|
| 53 |
|
| 54 |
const template = `{stlite_html_template.replace('STARTING_CODE', starting_app_code(demo_type))}`;
|
| 55 |
-
const frame = document.getElementById('
|
| 56 |
frame.contentWindow.document.open();
|
| 57 |
frame.contentWindow.document.write(template);
|
| 58 |
frame.contentWindow.document.close();
|
|
@@ -62,32 +64,20 @@ def load_js(demo_type: DemoType) -> str:
|
|
| 62 |
|
| 63 |
|
| 64 |
def update_iframe_js(demo_type: DemoType) -> str:
|
|
|
|
| 65 |
if demo_type == DemoType.GRADIO:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
const htmlString = '<iframe class="my-frame" width="100%" height="512px" src="about:blank"></iframe>';
|
| 76 |
-
const parser = new DOMParser();
|
| 77 |
-
const doc = parser.parseFromString(htmlString, 'text/html');
|
| 78 |
-
const iframe = doc.querySelector('.my-frame');
|
| 79 |
-
const div = document.getElementById('gradioDemoDiv');
|
| 80 |
-
div.appendChild(iframe);
|
| 81 |
-
|
| 82 |
-
const frame = document.querySelector('.my-frame');
|
| 83 |
-
frame.contentWindow.document.open('text/html', 'replace');
|
| 84 |
-
frame.contentWindow.document.write(completedTemplate);
|
| 85 |
-
frame.contentWindow.document.close();
|
| 86 |
-
}}"""
|
| 87 |
elif demo_type == DemoType.STREAMLIT:
|
| 88 |
return f"""async (code) => {{
|
| 89 |
async function update() {{
|
| 90 |
-
const appController = document.getElementById('
|
| 91 |
const newCode = code + ` # Update tag ${{Math.random()}}`;
|
| 92 |
const entrypointFile = "streamlit_app.py";
|
| 93 |
appController.writeFile(entrypointFile, newCode);
|
|
@@ -100,12 +90,10 @@ def update_iframe_js(demo_type: DemoType) -> str:
|
|
| 100 |
def copy_snippet_js(demo_type: DemoType) -> str:
|
| 101 |
if demo_type == DemoType.GRADIO:
|
| 102 |
return f"""async (code) => {{
|
| 103 |
-
const
|
| 104 |
-
const template =
|
| 105 |
-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.css" />
|
| 106 |
-
<gradio-lite>\n# APP CODE START\n\n# APP CODE END\n</gradio-lite>\n</div>\n`;
|
| 107 |
// Step 1: Generate the HTML content
|
| 108 |
-
const completedTemplate = template.replace(
|
| 109 |
|
| 110 |
const snippet = completedTemplate;
|
| 111 |
await navigator.clipboard.writeText(snippet);
|
|
@@ -113,7 +101,7 @@ def copy_snippet_js(demo_type: DemoType) -> str:
|
|
| 113 |
elif demo_type == DemoType.STREAMLIT:
|
| 114 |
return f"""async (code) => {{
|
| 115 |
const escapedCode = code.replace('`', String.fromCharCode(92) + '`');
|
| 116 |
-
const template = `{
|
| 117 |
// Step 1: Generate the HTML content
|
| 118 |
const completedTemplate = template.replace('STARTING_CODE', code);
|
| 119 |
|
|
@@ -126,25 +114,24 @@ def copy_snippet_js(demo_type: DemoType) -> str:
|
|
| 126 |
def download_code_js(demo_type: DemoType) -> str:
|
| 127 |
if demo_type == demo_type.GRADIO:
|
| 128 |
return f"""(code) => {{
|
| 129 |
-
const
|
| 130 |
-
const template = `{gradio_lite_html_template}`;
|
| 131 |
// Step 1: Generate the HTML content
|
| 132 |
-
const completedTemplate =
|
| 133 |
-
|
| 134 |
// Step 2: Create a Blob from the HTML content
|
| 135 |
const blob = new Blob([completedTemplate], {{ type: "text/html" }});
|
| 136 |
-
|
| 137 |
// Step 3: Create a URL for the Blob
|
| 138 |
const url = URL.createObjectURL(blob);
|
| 139 |
-
|
| 140 |
// Step 4: Create a download link
|
| 141 |
const downloadLink = document.createElement("a");
|
| 142 |
downloadLink.href = url;
|
| 143 |
downloadLink.download = "gradio-lite-app.html"; // Specify the filename for the download
|
| 144 |
-
|
| 145 |
// Step 5: Trigger a click event on the download link
|
| 146 |
downloadLink.click();
|
| 147 |
-
|
| 148 |
// Clean up by revoking the URL
|
| 149 |
URL.revokeObjectURL(url);
|
| 150 |
}}"""
|
|
|
|
| 9 |
|
| 10 |
gradio_lite_html_template = Path('templates/gradio-lite/gradio-lite-template.html').read_text()
|
| 11 |
stlite_html_template = Path('templates/stlite/stlite-template.html').read_text()
|
| 12 |
+
gradio_lite_snippet_template = Path('templates/gradio-lite/gradio-lite-snippet-template.html').read_text()
|
| 13 |
stlite_snippet_template = Path('templates/stlite/stlite-snippet-template.html').read_text()
|
| 14 |
|
| 15 |
|
| 16 |
def starting_app_code(demo_type: DemoType) -> str:
|
| 17 |
if demo_type == DemoType.GRADIO:
|
| 18 |
+
return Path('templates/gradio-lite/gradio_lite_starting_code.py').read_text().replace('`', r'\`')
|
| 19 |
elif demo_type == DemoType.STREAMLIT:
|
| 20 |
return Path('templates/stlite/stlite_starting_code.py').read_text().replace('`', r'\`')
|
| 21 |
raise NotImplementedError(f'{demo_type} is not a supported demo type')
|
|
|
|
| 27 |
if (window.gradioLiteLoaded) {{
|
| 28 |
return
|
| 29 |
}}
|
| 30 |
+
const htmlString = '<iframe id="gradio-iframe" width="100%" height="512px" src="about:blank"></iframe>';
|
| 31 |
const parser = new DOMParser();
|
| 32 |
const doc = parser.parseFromString(htmlString, 'text/html');
|
| 33 |
+
const iframe = doc.getElementById('gradio-iframe');
|
| 34 |
const div = document.getElementById('gradioDemoDiv');
|
| 35 |
div.appendChild(iframe);
|
| 36 |
|
| 37 |
+
const template = `{gradio_lite_html_template.replace('STARTING_CODE', starting_app_code(demo_type))}`;
|
| 38 |
+
const frame = document.getElementById('gradio-iframe');
|
| 39 |
frame.contentWindow.document.open('text/html', 'replace');
|
| 40 |
+
frame.contentWindow.document.write(template);
|
| 41 |
frame.contentWindow.document.close();
|
| 42 |
window.gradioLiteLoaded = true;
|
| 43 |
}}"""
|
|
|
|
| 46 |
if (window.stliteLoaded) {{
|
| 47 |
return
|
| 48 |
}}
|
| 49 |
+
const htmlString = '<iframe id="stlite-iframe" width="100%" height="512px" src="about:blank"></iframe>';
|
| 50 |
const parser = new DOMParser();
|
| 51 |
const doc = parser.parseFromString(htmlString, 'text/html');
|
| 52 |
+
const iframe = doc.getElementById('stlite-iframe');
|
| 53 |
const div = document.getElementById('stliteDemoDiv');
|
| 54 |
div.appendChild(iframe);
|
| 55 |
|
| 56 |
const template = `{stlite_html_template.replace('STARTING_CODE', starting_app_code(demo_type))}`;
|
| 57 |
+
const frame = document.getElementById('stlite-iframe');
|
| 58 |
frame.contentWindow.document.open();
|
| 59 |
frame.contentWindow.document.write(template);
|
| 60 |
frame.contentWindow.document.close();
|
|
|
|
| 64 |
|
| 65 |
|
| 66 |
def update_iframe_js(demo_type: DemoType) -> str:
|
| 67 |
+
# TODO: Theme changes are not picked up; look at options to refresh the css.
|
| 68 |
if demo_type == DemoType.GRADIO:
|
| 69 |
+
return f"""async (code) => {{
|
| 70 |
+
async function update() {{
|
| 71 |
+
const appController = document.getElementById('gradio-iframe').contentWindow.window.appController;
|
| 72 |
+
const newCode = code + ` # Update tag ${{Math.random()}}`;
|
| 73 |
+
appController.run_code(newCode);
|
| 74 |
+
}};
|
| 75 |
+
await update();
|
| 76 |
+
}}"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
elif demo_type == DemoType.STREAMLIT:
|
| 78 |
return f"""async (code) => {{
|
| 79 |
async function update() {{
|
| 80 |
+
const appController = document.getElementById('stlite-iframe').contentWindow.window.appController;
|
| 81 |
const newCode = code + ` # Update tag ${{Math.random()}}`;
|
| 82 |
const entrypointFile = "streamlit_app.py";
|
| 83 |
appController.writeFile(entrypointFile, newCode);
|
|
|
|
| 90 |
def copy_snippet_js(demo_type: DemoType) -> str:
|
| 91 |
if demo_type == DemoType.GRADIO:
|
| 92 |
return f"""async (code) => {{
|
| 93 |
+
const escapedCode = code.replace('`', String.fromCharCode(92) + '`');
|
| 94 |
+
const template = `{gradio_lite_snippet_template}`;
|
|
|
|
|
|
|
| 95 |
// Step 1: Generate the HTML content
|
| 96 |
+
const completedTemplate = template.replace('STARTING_CODE', code);
|
| 97 |
|
| 98 |
const snippet = completedTemplate;
|
| 99 |
await navigator.clipboard.writeText(snippet);
|
|
|
|
| 101 |
elif demo_type == DemoType.STREAMLIT:
|
| 102 |
return f"""async (code) => {{
|
| 103 |
const escapedCode = code.replace('`', String.fromCharCode(92) + '`');
|
| 104 |
+
const template = `{stlite_snippet_template}`;
|
| 105 |
// Step 1: Generate the HTML content
|
| 106 |
const completedTemplate = template.replace('STARTING_CODE', code);
|
| 107 |
|
|
|
|
| 114 |
def download_code_js(demo_type: DemoType) -> str:
|
| 115 |
if demo_type == demo_type.GRADIO:
|
| 116 |
return f"""(code) => {{
|
| 117 |
+
const escapedCode = code.replace('`', String.fromCharCode(92) + '`');
|
|
|
|
| 118 |
// Step 1: Generate the HTML content
|
| 119 |
+
const completedTemplate = `{gradio_lite_html_template}`.replace('STARTING_CODE', escapedCode);
|
| 120 |
+
|
| 121 |
// Step 2: Create a Blob from the HTML content
|
| 122 |
const blob = new Blob([completedTemplate], {{ type: "text/html" }});
|
| 123 |
+
|
| 124 |
// Step 3: Create a URL for the Blob
|
| 125 |
const url = URL.createObjectURL(blob);
|
| 126 |
+
|
| 127 |
// Step 4: Create a download link
|
| 128 |
const downloadLink = document.createElement("a");
|
| 129 |
downloadLink.href = url;
|
| 130 |
downloadLink.download = "gradio-lite-app.html"; // Specify the filename for the download
|
| 131 |
+
|
| 132 |
// Step 5: Trigger a click event on the download link
|
| 133 |
downloadLink.click();
|
| 134 |
+
|
| 135 |
// Clean up by revoking the URL
|
| 136 |
URL.revokeObjectURL(url);
|
| 137 |
}}"""
|
templates/gradio-lite/gradio-lite-snippet-template.html
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<div id="KiteWindApp">
|
| 2 |
+
<iframe id="demo-iframe" width="100%" height="512px" src="about:blank" frameborder="0"></iframe>
|
| 3 |
+
<script>
|
| 4 |
+
const template = \`<div id="gradio-app"></div>
|
| 5 |
+
<script type="module" crossorigin src="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.js"><\\\/script>
|
| 6 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.css" />
|
| 7 |
+
<script type="module">
|
| 8 |
+
const code = \\\`STARTING_CODE\\\`;
|
| 9 |
+
const appController = createGradioApp({
|
| 10 |
+
target: document.getElementById("gradio-app"),
|
| 11 |
+
code: code,
|
| 12 |
+
info: true,
|
| 13 |
+
container: true,
|
| 14 |
+
isEmbed: false,
|
| 15 |
+
initialHeight: "400px",
|
| 16 |
+
eager: false,
|
| 17 |
+
themeMode: null,
|
| 18 |
+
autoScroll: false,
|
| 19 |
+
controlPageTitle: false,
|
| 20 |
+
appMode: true
|
| 21 |
+
});
|
| 22 |
+
window.appController = appController;
|
| 23 |
+
<\\\/script>\`
|
| 24 |
+
const frame = document.getElementById('demo-iframe');
|
| 25 |
+
frame.contentWindow.document.open();
|
| 26 |
+
frame.contentWindow.document.write(template);
|
| 27 |
+
frame.contentWindow.document.close();
|
| 28 |
+
</script>
|
| 29 |
+
</div>
|
templates/gradio-lite/gradio-lite-template.html
CHANGED
|
@@ -5,25 +5,26 @@
|
|
| 5 |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.css" />
|
| 6 |
</head>
|
| 7 |
<body>
|
| 8 |
-
<
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
#
|
| 27 |
-
|
|
|
|
| 28 |
</body>
|
| 29 |
</html>
|
|
|
|
| 5 |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.css" />
|
| 6 |
</head>
|
| 7 |
<body>
|
| 8 |
+
<div id="gradio-app"></div>
|
| 9 |
+
<script type="module">
|
| 10 |
+
const code = \`STARTING_CODE\`;
|
| 11 |
+
// Mount options defined here: https://github.com/gradio-app/gradio/blob/1d986217f6f4fc1829e528d2afe365635788204f/js/lite/index.html#L27
|
| 12 |
+
const appController = createGradioApp({
|
| 13 |
+
target: document.getElementById("gradio-app"),
|
| 14 |
+
code: code,
|
| 15 |
+
info: true,
|
| 16 |
+
container: true,
|
| 17 |
+
isEmbed: false,
|
| 18 |
+
initialHeight: "400px",
|
| 19 |
+
eager: false,
|
| 20 |
+
themeMode: null,
|
| 21 |
+
autoScroll: false,
|
| 22 |
+
controlPageTitle: false,
|
| 23 |
+
appMode: true
|
| 24 |
+
});
|
| 25 |
+
// Returned controller functions defined here:
|
| 26 |
+
// https://github.com/gradio-app/gradio/blob/1d986217f6f4fc1829e528d2afe365635788204f/js/app/src/lite/index.ts#L165
|
| 27 |
+
window.appController = appController;
|
| 28 |
+
</script>
|
| 29 |
</body>
|
| 30 |
</html>
|
templates/stlite/stlite-snippet-template.html
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
<div id="KiteWindApp">
|
| 2 |
-
<iframe id="demo-iframe" width="100%" height="512px" src="about:blank"></iframe>
|
| 3 |
<script>
|
| 4 |
const template = \`<div id="root"></div>
|
| 5 |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@stlite/mountable@0.39.0/build/stlite.css"/>
|
|
|
|
| 1 |
<div id="KiteWindApp">
|
| 2 |
+
<iframe id="demo-iframe" width="100%" height="512px" src="about:blank" frameborder="0"></iframe>
|
| 3 |
<script>
|
| 4 |
const template = \`<div id="root"></div>
|
| 5 |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@stlite/mountable@0.39.0/build/stlite.css"/>
|