main.py 4.0 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 apply_tailwind
  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 = 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.add_head_html('<meta name="viewport" content="width=device-width, initial-scale=1" />')
  30. ui.html('''
  31. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-fork-ribbon-css/0.2.3/gh-fork-ribbon.min.css" />
  32. <style>
  33. .github-fork-ribbon:before { background-color: #999; }
  34. </style>
  35. <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>
  36. ''')
  37. installation_start = README.find('<h2 class="text-4xl mb-3 mt-5">Installation</h2>')
  38. documentation_start = README.find('The API reference is hosted at')
  39. assert installation_start >= 0
  40. assert documentation_start >= 0
  41. with ui.row().classes('flex w-full overflow-scroll'):
  42. ui.html(README[:installation_start]).classes('w-1/2 flex-grow')
  43. with ui.column().classes('flex-none items-center'):
  44. with ui.card(), ui.row().classes('w-96'):
  45. with ui.column().classes('w-5/12'):
  46. ui.button('Click me!', on_click=lambda: output.set_text('Click'))
  47. ui.checkbox('Check me!', on_change=lambda e: output.set_text('Checked' if e.value else 'Unchecked'))
  48. ui.switch('Switch me!', on_change=lambda e: output.set_text(
  49. 'Switched' if e.value else 'Unswitched'))
  50. ui.input('Text', value='abc', on_change=lambda e: output.set_text(e.value))
  51. ui.number('Number', value=3.1415927, format='%.2f', on_change=lambda e: output.set_text(e.value))
  52. with ui.column().classes('w-6/12'):
  53. ui.slider(min=0, max=100, value=50, step=0.1, on_change=lambda e: output.set_text(e.value))
  54. ui.radio(['A', 'B', 'C'], value='A', on_change=lambda e: output.set_text(e.value)).props('inline')
  55. ui.toggle(['1', '2', '3'], value='1', on_change=lambda e: output.set_text(e.value))
  56. ui.select({1: 'One', 2: 'Two', 3: 'Three'}, value=1, on_change=lambda e: output.set_text(e.value))
  57. with ui.row().classes('mt-8'):
  58. ui.label('Output:')
  59. output = ui.label().classes('text-bold')
  60. traffic_tracking.chart().classes('mt-8 w-full h-64')
  61. ui.html(README[installation_start:documentation_start]).classes('w-full')
  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:]).classes('w-full')
  66. @ui.page('/reference')
  67. def reference():
  68. ui.add_head_html('<meta name="viewport" content="width=device-width, initial-scale=1" />')
  69. api_docs_and_examples.create_full()
  70. ui.run()