#!/usr/bin/env python3 import re import markdown2 import api_docs_and_examples import traffic_tracking from nicegui import ui from nicegui.elements.markdown import Markdown with open('README.md') as f: content = f.read() content = re.sub(r'(?m)^\ None: # NOTE because the docs are added after initial page load, we need to manually trigger the jump to the anchor await ui.run_javascript(''' parts = document.URL.split("#"); console.log(parts); if (parts.length > 1) { console.log(window.location); window.location = parts[0] + "reference#" + parts[1]; console.log(window.location); } ''') @ui.page('/', on_connect=traffic_tracking.on_connect, on_page_ready=go_to_anchor) async def index(): # avoid display:block for PyPI/Docker/GitHub badges ui.add_head_html('') ui.add_head_html('') ui.html(''' Fork me on GitHub ''') installation_start = README.find('

Installation

') documentation_start = README.find('The API reference is hosted at') assert installation_start >= 0 assert documentation_start >= 0 with ui.row().classes('flex w-full overflow-scroll'): ui.html(README[:installation_start]).classes('w-1/2 flex-grow') with ui.column().classes('flex-none items-center'): with ui.card(), ui.row().classes('w-96'): with ui.column().classes('w-5/12'): ui.button('Click me!', on_click=lambda: output.set_text('Click')) ui.checkbox('Check me!', on_change=lambda e: output.set_text('Checked' if e.value else 'Unchecked')) ui.switch('Switch me!', on_change=lambda e: output.set_text( 'Switched' if e.value else 'Unswitched')) ui.input('Text', value='abc', on_change=lambda e: output.set_text(e.value)) ui.number('Number', value=3.1415927, format='%.2f', on_change=lambda e: output.set_text(e.value)) with ui.column().classes('w-6/12'): ui.slider(min=0, max=100, value=50, step=0.1, on_change=lambda e: output.set_text(e.value)) ui.radio(['A', 'B', 'C'], value='A', on_change=lambda e: output.set_text(e.value)).props('inline') ui.toggle(['1', '2', '3'], value='1', on_change=lambda e: output.set_text(e.value)) ui.select({1: 'One', 2: 'Two', 3: 'Three'}, value=1, on_change=lambda e: output.set_text(e.value)) with ui.row().classes('mt-8'): ui.label('Output:') output = ui.label().classes('text-bold') traffic_tracking.chart().classes('mt-8 w-full h-64') ui.html(README[installation_start:documentation_start]).classes('w-full') api_docs_and_examples.create_intro() with ui.row().style('background-color: #e8f0fa; width: 100%; margin: 1em 0; padding: 1em 1em 0.5em 1em; font-size: large'): ui.markdown('See the [API reference](/reference) for many more interactive examples!') ui.html(README[documentation_start:]).classes('w-full') @ui.page('/reference') def reference(): ui.add_head_html('') api_docs_and_examples.create_full() ui.run()