from pathlib import Path gradio_lite_html_template = Path('templates/gradio-lite/gradio-lite-template.html').read_text() stlite_html_template = Path('templates/stlite/stlite-template.html').read_text() stlite_snippet_template = Path('templates/stlite/stlite-snippet-template.html').read_text() def starting_app_code(demo_type): if demo_type == 'gradio-lite': return Path('templates/gradio-lite/gradio_lite_starting_code.py').read_text() elif demo_type == 'stlite': return Path('templates/stlite/stlite_starting_code.py').read_text().replace('`', r'\`') raise NotImplementedError(f'{demo_type} is not a supported demo type') def load_js(demo_type): if demo_type == 'gradio-lite': return f"""() => {{ const htmlString = ''; const parser = new DOMParser(); const doc = parser.parseFromString(htmlString, 'text/html'); const iframe = doc.querySelector('.my-frame'); const div = document.getElementById('gradioDemoDiv'); div.appendChild(iframe); const frame = document.querySelector('.my-frame'); frame.contentWindow.document.open('text/html', 'replace'); frame.contentWindow.document.write(`{gradio_lite_html_template}`); frame.contentWindow.document.close(); }}""" elif demo_type == 'stlite': return f"""() => {{ const htmlString = ''; const parser = new DOMParser(); const doc = parser.parseFromString(htmlString, 'text/html'); const iframe = doc.getElementById('demo-iframe'); const div = document.getElementById('stliteDemoDiv'); div.appendChild(iframe); const template = `{stlite_html_template.replace('STARTING_CODE', starting_app_code(demo_type))}`; console.log(template); const frame = document.getElementById('demo-iframe'); frame.contentWindow.document.open(); frame.contentWindow.document.write(template); frame.contentWindow.document.close(); }}""" raise NotImplementedError(f'{demo_type} is not a supported demo type') def update_iframe_js(demo_type): if demo_type == 'gradio-lite': # TODO: Works but is inefficient because the iframe has to be reloaded each time return f"""(code) => {{ const pattern = /# APP CODE START(.*?)# APP CODE END/gs; const template = `{gradio_lite_html_template}`; const completedTemplate = template.replace(pattern, code); const oldFrame = document.querySelector('.my-frame'); oldFrame.remove(); const htmlString = ''; const parser = new DOMParser(); const doc = parser.parseFromString(htmlString, 'text/html'); const iframe = doc.querySelector('.my-frame'); const div = document.getElementById('gradioDemoDiv'); div.appendChild(iframe); const frame = document.querySelector('.my-frame'); frame.contentWindow.document.open('text/html', 'replace'); frame.contentWindow.document.write(completedTemplate); frame.contentWindow.document.close(); }}""" elif demo_type == 'stlite': return f"""async (code) => {{ async function update() {{ const appController = document.getElementById('demo-iframe').contentWindow.window.appController; const newCode = code + ` # Update tag ${{Math.random()}}`; const entrypointFile = "streamlit_app.py"; appController.writeFile(entrypointFile, newCode); }}; await update(); }}""" raise NotImplementedError(f'{demo_type} is not a supported demo type') def copy_snippet_js(demo_type): if demo_type == 'gradio-lite': return f"""async (code) => {{ const pattern = /# APP CODE START(.*?)# APP CODE END/gs; const template = `