main.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. # add docutils css to webpage
  29. ui.add_head_html(docutils.core.publish_parts('', writer_name='html')['stylesheet'])
  30. # avoid display:block for PyPI/Docker/GitHub badges
  31. ui.add_head_html('<style>p a img {display: inline; vertical-align: baseline}</style>')
  32. ui.html(
  33. '<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-fork-ribbon-css/0.2.3/gh-fork-ribbon.min.css" />'
  34. '<style>.github-fork-ribbon:before { background-color: #999; }</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. with ui.row().classes('flex w-full'):
  38. ui.html(README).classes('w-6/12')
  39. with ui.column().classes('w-5/12 flex-center'):
  40. width = 450
  41. with ui.card(), ui.row().style(f'width:{width}px'):
  42. with ui.column():
  43. ui.button('Click me!', on_click=lambda: output.set_text('Click'))
  44. ui.checkbox('Check me!', on_change=lambda e: output.set_text('Checked' if e.value else 'Unchecked'))
  45. ui.switch('Switch me!', on_change=lambda e: output.set_text(
  46. 'Switched' if e.value else 'Unswitched'))
  47. ui.input('Text', value='abc', on_change=lambda e: output.set_text(e.value))
  48. ui.number('Number', value=3.1415927, format='%.2f', on_change=lambda e: output.set_text(e.value))
  49. with ui.column():
  50. ui.slider(min=0, max=100, value=50, step=0.1, on_change=lambda e: output.set_text(e.value))
  51. ui.radio(['A', 'B', 'C'], value='A', on_change=lambda e: output.set_text(e.value)).props('inline')
  52. ui.toggle(['1', '2', '3'], value='1', on_change=lambda e: output.set_text(e.value)).classes('mx-auto')
  53. ui.select({1: 'One', 2: 'Two', 3: 'Three'}, value=1,
  54. on_change=lambda e: output.set_text(e.value)).classes('mx-auto')
  55. with ui.column().classes('w-24'):
  56. ui.label('Output:')
  57. output = ui.label('').classes('text-bold')
  58. with ui.row().style('margin-top: 40px'):
  59. traffic_tracking.chart().style(f'width:{width}px;height:250px')
  60. @ui.page('/reference')
  61. def reference():
  62. api_docs_and_examples.create()
  63. ui.run()