1
0

main.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python3
  2. import re
  3. import docutils.core
  4. import markdown2
  5. import api_docs_and_examples
  6. import traffic_tracking
  7. from nicegui import ui
  8. from nicegui.elements.markdown import Markdown
  9. with open('README.md') as f:
  10. content = f.read()
  11. content = re.sub(r'(?m)^\<img.*\n?', '', content)
  12. # change absolute link on GitHub to relative link
  13. content = content.replace('(https://nicegui.io/reference)', '(reference)')
  14. README = Markdown.apply_tailwind(markdown2.markdown(content, extras=['fenced-code-blocks']))
  15. async def go_to_anchor() -> None:
  16. # NOTE because the docs are added after initial page load, we need to manually trigger the jump to the anchor
  17. await ui.run_javascript('''
  18. parts = document.URL.split("#");
  19. console.log(parts);
  20. if (parts.length > 1) {
  21. console.log(window.location);
  22. window.location = parts[0] + "reference#" + parts[1];
  23. console.log(window.location);
  24. }
  25. ''')
  26. @ui.page('/', on_connect=traffic_tracking.on_connect, on_page_ready=go_to_anchor)
  27. async def index():
  28. # avoid display:block for PyPI/Docker/GitHub badges
  29. ui.add_head_html('<style>p a img {display: inline; vertical-align: baseline}</style>')
  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>.github-fork-ribbon:before { background-color: #999; }</style>'
  33. '<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>'
  34. )
  35. with ui.row().classes('flex w-full'):
  36. ui.html(README).classes('w-6/12')
  37. with ui.column().classes('w-5/12 flex-center'):
  38. width = 450
  39. with ui.card(), ui.row().style(f'width:{width}px'):
  40. with ui.column():
  41. ui.button('Click me!', on_click=lambda: output.set_text('Click'))
  42. ui.checkbox('Check me!', on_change=lambda e: output.set_text('Checked' if e.value else 'Unchecked'))
  43. ui.switch('Switch me!', on_change=lambda e: output.set_text(
  44. 'Switched' if e.value else 'Unswitched'))
  45. ui.input('Text', value='abc', on_change=lambda e: output.set_text(e.value))
  46. ui.number('Number', value=3.1415927, format='%.2f', on_change=lambda e: output.set_text(e.value))
  47. with ui.column():
  48. ui.slider(min=0, max=100, value=50, step=0.1, on_change=lambda e: output.set_text(e.value))
  49. ui.radio(['A', 'B', 'C'], value='A', on_change=lambda e: output.set_text(e.value)).props('inline')
  50. ui.toggle(['1', '2', '3'], value='1', on_change=lambda e: output.set_text(e.value)).classes('mx-auto')
  51. ui.select({1: 'One', 2: 'Two', 3: 'Three'}, value=1,
  52. on_change=lambda e: output.set_text(e.value)).classes('mx-auto')
  53. with ui.column().classes('w-24'):
  54. ui.label('Output:')
  55. output = ui.label('').classes('text-bold')
  56. with ui.row().style('margin-top: 40px'):
  57. traffic_tracking.chart().style(f'width:{width}px;height:250px')
  58. @ui.page('/reference')
  59. def reference():
  60. api_docs_and_examples.create()
  61. ui.run()