main.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python3
  2. import re
  3. import markdown2
  4. import api_docs_and_examples
  5. import traffic_tracking
  6. from nicegui import ui
  7. from nicegui.elements.markdown import Markdown
  8. with open('README.md') as f:
  9. content = f.read()
  10. content = re.sub(r'(?m)^\<img.*\n?', '', content)
  11. # change absolute link on GitHub to relative link
  12. content = content.replace('(https://nicegui.io/reference)', '(reference)')
  13. README = Markdown.apply_tailwind(markdown2.markdown(content, extras=['fenced-code-blocks']))
  14. async def go_to_anchor() -> None:
  15. # NOTE because the docs are added after initial page load, we need to manually trigger the jump to the anchor
  16. await ui.run_javascript('''
  17. parts = document.URL.split("#");
  18. console.log(parts);
  19. if (parts.length > 1) {
  20. console.log(window.location);
  21. window.location = parts[0] + "reference#" + parts[1];
  22. console.log(window.location);
  23. }
  24. ''')
  25. @ui.page('/', on_connect=traffic_tracking.on_connect, on_page_ready=go_to_anchor)
  26. async def index():
  27. # avoid display:block for PyPI/Docker/GitHub badges
  28. ui.add_head_html('<style>p a img {display: inline; vertical-align: baseline}</style>')
  29. ui.html(
  30. '<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-fork-ribbon-css/0.2.3/gh-fork-ribbon.min.css" />'
  31. '<style>.github-fork-ribbon:before { background-color: #999; }</style>'
  32. '<a class="github-fork-ribbon" href="https://github.com/zauberzeug/nicegui" data-ribbon="Fork me on GitHub" title="Fork me on GitHub">Fork me on GitHub</a>'
  33. )
  34. installation_start = README.find('<h2 class="text-4xl mb-3 mt-5">Installation</h2>')
  35. documentation_start = README.find('The API reference is hosted at')
  36. assert installation_start >= 0
  37. assert documentation_start >= 0
  38. with ui.row().classes('flex w-full'):
  39. ui.html(README[:installation_start]).classes('w-6/12')
  40. with ui.column().classes('w-5/12 flex-center'):
  41. width = 450
  42. with ui.card(), ui.row().style(f'width:{width}px'):
  43. with ui.column():
  44. ui.button('Click me!', on_click=lambda: output.set_text('Click'))
  45. ui.checkbox('Check me!', on_change=lambda e: output.set_text('Checked' if e.value else 'Unchecked'))
  46. ui.switch('Switch me!', on_change=lambda e: output.set_text(
  47. 'Switched' if e.value else 'Unswitched'))
  48. ui.input('Text', value='abc', on_change=lambda e: output.set_text(e.value))
  49. ui.number('Number', value=3.1415927, format='%.2f', on_change=lambda e: output.set_text(e.value))
  50. with ui.column():
  51. ui.slider(min=0, max=100, value=50, step=0.1, on_change=lambda e: output.set_text(e.value))
  52. ui.radio(['A', 'B', 'C'], value='A', on_change=lambda e: output.set_text(e.value)).props('inline')
  53. ui.toggle(['1', '2', '3'], value='1', on_change=lambda e: output.set_text(e.value)).classes('mx-auto')
  54. ui.select({1: 'One', 2: 'Two', 3: 'Three'}, value=1,
  55. on_change=lambda e: output.set_text(e.value)).classes('mx-auto')
  56. with ui.column().classes('w-24'):
  57. ui.label('Output:')
  58. output = ui.label('').classes('text-bold')
  59. with ui.row().style('margin-top: 40px'):
  60. traffic_tracking.chart().style(f'width:{width}px;height:250px')
  61. ui.html(README[installation_start:documentation_start])
  62. api_docs_and_examples.create_intro()
  63. with ui.row().style('background-color: #e8f0fa; width: 100%; margin: 1em 0; padding: 1em 1em 0.5em 1em; font-size: large'):
  64. ui.markdown('See the [API reference](/reference) for many more interactive examples!')
  65. ui.html(README[documentation_start:])
  66. @ui.page('/reference')
  67. def reference():
  68. api_docs_and_examples.create_full()
  69. ui.run()