Repository: h2oai/wave
File: web-ide/web_ide.py (Python)

Code before:
    await stop(q.user.wave_process)
    q.user.wave_process = await start(filename, is_app)

    del q.page['empty']
    q.page['preview'] = ui.frame_card(
        box='preview',
        title=f'Preview of {_server_adress}{path}',
        path=f'{_server_adress}{path}'
    )


async def on_startup():
    # Clean up previous tmp dir.
    await on_shutdown()
    os.mkdir(tmp_dir)


async def on_shutdown():
    dirpath = Path(tmp_dir)
    if dirpath.exists():
        shutil.rmtree(dirpath)

async def export(q: Q):
    shutil.make_archive('app', 'zip', '.', 'tmp_project')
    zip_path = await q.site.upload(['app.zip'])
    q.app.zip_path = f"{_server_adress}{zip_path[0]}"

@app('/ide', on_startup=on_startup, on_shutdown=on_shutdown)
async def serve(q: Q):
    if q.args.export:
        await export(q)
        q.page["meta"].script = ui.inline_script(f"""window.open("{q.app.zip_path}", "_blank");""")
        os.remove("app.zip")
    else:
        if not q.app.initialized:
            # Prod.
            if os.path.exists('base-snippets.json') and os.path.exists('component-snippets.json'):
                q.app.snippets1, q.app.snippets2, = await q.site.upload(['base-snippets.json', 'component-snippets.json'])
            # When run in development from Wave repo.
            elif os.path.exists(vsc_extension_path):
                q.app.snippets1, q.app.snippets2, = await q.site.upload([
                    os.path.join(vsc_extension_path, 'base-snippets.json'),
                    os.path.join(vsc_extension_path, 'component-snippets.json')
                ])
            q.app.app_not_running_img, = await q.site.upload([os.path.join('assets', 'app_not_running.svg')])
            q.app.initialized = True
        if not q.client.initialized:
            await setup_page(q)
            q.client.initialized = True
        await render_code(q)


Review comment (suggestion):
I guess this shall work as well, please verify.
```suggestion
    q.app.zip_path, = await q.site.upload(['app.zip'])
```

Code after:

    await stop(q.user.wave_process)
    q.user.wave_process = await start(filename, is_app)

    del q.page['empty']
    q.page['preview'] = ui.frame_card(
        box='preview',
        title=f'Preview of {_server_adress}{path}',
        path=f'{_server_adress}{path}'
    )


async def on_startup():
    # Clean up previous tmp dir.
    await on_shutdown()
    os.mkdir(tmp_dir)


async def on_shutdown():
    dirpath = Path(tmp_dir)
    if dirpath.exists():
        shutil.rmtree(dirpath)

async def export(q: Q):
    shutil.make_archive('app', 'zip', '.', 'tmp_project')
    q.app.zip_path, = await q.site.upload(['app.zip'])
    q.page["meta"].script = ui.inline_script(f"""window.open("{q.app.zip_path}", "_blank");""")
    os.remove("app.zip")

@app('/ide', on_startup=on_startup, on_shutdown=on_shutdown)
async def serve(q: Q):
    if q.args.export:
        await export(q)
    if not q.app.initialized:
        # Prod.
        if os.path.exists('base-snippets.json') and os.path.exists('component-snippets.json'):
            q.app.snippets1, q.app.snippets2, = await q.site.upload(['base-snippets.json', 'component-snippets.json'])
        # When run in development from Wave repo.
        elif os.path.exists(vsc_extension_path):
            q.app.snippets1, q.app.snippets2, = await q.site.upload([
                os.path.join(vsc_extension_path, 'base-snippets.json'),
                os.path.join(vsc_extension_path, 'component-snippets.json')
            ])
        q.app.app_not_running_img, = await q.site.upload([os.path.join('assets', 'app_not_running.svg')])
        q.app.initialized = True
    if not q.client.initialized:
        await setup_page(q)
        q.client.initialized = True
    if q.events.editor:
        await render_code(q)