main.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. with ui.row().classes('flex w-full'):
  35. ui.html(README).classes('w-6/12')
  36. with ui.column().classes('w-5/12 flex-center'):
  37. width = 450
  38. with ui.card(), ui.row().style(f'width:{width}px'):
  39. with ui.column():
  40. ui.button('Click me!', on_click=lambda: output.set_text('Click'))
  41. ui.checkbox('Check me!', on_change=lambda e: output.set_text('Checked' if e.value else 'Unchecked'))
  42. ui.switch('Switch me!', on_change=lambda e: output.set_text(
  43. 'Switched' if e.value else 'Unswitched'))
  44. ui.input('Text', value='abc', on_change=lambda e: output.set_text(e.value))
  45. ui.number('Number', value=3.1415927, format='%.2f', on_change=lambda e: output.set_text(e.value))
  46. with ui.column():
  47. ui.slider(min=0, max=100, value=50, step=0.1, on_change=lambda e: output.set_text(e.value))
  48. ui.radio(['A', 'B', 'C'], value='A', on_change=lambda e: output.set_text(e.value)).props('inline')
  49. ui.toggle(['1', '2', '3'], value='1', on_change=lambda e: output.set_text(e.value)).classes('mx-auto')
  50. ui.select({1: 'One', 2: 'Two', 3: 'Three'}, value=1,
  51. on_change=lambda e: output.set_text(e.value)).classes('mx-auto')
  52. with ui.column().classes('w-24'):
  53. ui.label('Output:')
  54. output = ui.label('').classes('text-bold')
  55. with ui.row().style('margin-top: 40px'):
  56. traffic_tracking.chart().style(f'width:{width}px;height:250px')
  57. @ui.page('/reference')
  58. def reference():
  59. api_docs_and_examples.create()
  60. ui.run()