Repository: h2oai/wave
File: py/examples/tour.py (Python)

Code before:
        title='',
        content='<div id="monaco-editor" style="position: absolute; top: 45px; bottom: 15px; right: 15px; left: 2px"/>'
    )
    # Put tmp placeholder <div></div> to simulate blank screen.
    q.page['preview'] = ui.frame_card(box='preview', title='Preview', content='<div></div>')
    await q.page.save()


def make_blurb(q: Q):
    example = q.user.active_example
    blurb_card = q.page['blurb']
    blurb_card.title = example.title
    blurb_card.subtitle = example.description
    # HACK: Recreate dropdown every time (by dynamic name) to control value (needed for next / prev btn functionality).
    items = [ui.dropdown(name=q.args['#'] or default_example_name, width='300px', value=example.name, trigger=True,
                         choices=[ui.choice(name=e.name, label=e.title) for e in catalog.values()])]
    buttons = []
    mobile_buttons = []
    if example.previous_example:
        buttons.append(ui.button(name=f'#{example.previous_example.name}', label='Previous'))
        mobile_buttons.append(ui.button(name=f'#{example.previous_example.name}', label='Prev'))
    if example.next_example:
        buttons.append(ui.button(name=f'#{example.next_example.name}', label='Next', primary=True))
        mobile_buttons.append(ui.button(name=f'#{example.next_example.name}', label='Next', primary=True))
    blurb_card.items = items + buttons
    q.page['mobile_blurb'].items = [ui.inline(direction='row', justify='center', items=items + mobile_buttons)]


async def show_example(q: Q, example: Example):
    # Clear demo page
    demo_page = q.site['/demo']
    demo_page.drop()
    await demo_page.save()

    filename = os.path.join(tour_tmp_dir, 'tmp.py')
    code = q.events.editor.change if q.events.editor else example.source
    code = code.replace("`", "\\`")
    with open(filename, 'w') as f:
        f.write(code)
    filename = '.'.join([tour_tmp_dir, 'tmp.py']).split(os.sep)[-1] if code.find('@app(') > 0 else filename

    # Stop active example, if any.
    active_example = q.user.active_example
    if active_example:
        await active_example.stop()

    # Start new example
    await example.start(filename, code)
    q.user.active_example = example

    # Update example blurb

Review comment (bug):
Let's simplify this. No need for extra `buttons` and `mobile_buttons` as it seems like there is a difference only in `label` (correct me if I am wrong).

GH won't let me add suggestions on deleted lines so here is the whole code.

```py
    if example.previous_example:
        items.append(ui.button(name=f'#{example.previous_example.name}', label='Prev'))
    if example.next_example:
        items.append(ui.button(name=f'#{example.next_example.name}', label='Next', primary=True))
    q.page['mobile_blurb'].items = [ui.inline(direction='row', justify='center', items=items)]
```

Code after:
        title='',
        content='<div id="monaco-editor" style="position: absolute; top: 45px; bottom: 15px; right: 15px; left: 2px"/>'
    )
    # Put tmp placeholder <div></div> to simulate blank screen.
    q.page['preview'] = ui.frame_card(box='preview', title='Preview', content='<div></div>')
    await q.page.save()


def make_blurb(q: Q):
    example = q.user.active_example
    blurb_card = q.page['blurb']
    blurb_card.title = example.title
    blurb_card.subtitle = example.description
    # HACK: Recreate dropdown every time (by dynamic name) to control value (needed for next / prev btn functionality).
    items = [ui.dropdown(name=q.args['#'] or default_example_name, width='300px', value=example.name, trigger=True,
                         choices=[ui.choice(name=e.name, label=e.title) for e in catalog.values()])]
    if example.previous_example:
        items.append(ui.button(name=f'#{example.previous_example.name}', label='Prev'))
    if example.next_example:
        items.append(ui.button(name=f'#{example.next_example.name}', label='Next', primary=True))
    blurb_card.items = items
    q.page['mobile_blurb'].items = [ui.inline(direction='row', justify='center', items=items)]


async def show_example(q: Q, example: Example):
    # Clear demo page
    demo_page = q.site['/demo']
    demo_page.drop()
    await demo_page.save()

    filename = os.path.join(tour_tmp_dir, 'tmp.py')
    code = q.events.editor.change if q.events.editor else example.source
    code = code.replace("`", "\\`")
    with open(filename, 'w') as f:
        f.write(code)
    filename = '.'.join([tour_tmp_dir, 'tmp.py']).split(os.sep)[-1] if code.find('@app(') > 0 else filename

    # Stop active example, if any.
    active_example = q.user.active_example
    if active_example:
        await active_example.stop()

    # Start new example
    await example.start(filename, code)
    q.user.active_example = example

    # Update example blurb
    make_blurb(q)

    preview_card = q.page['preview']