markdown_documentation.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.markdown)
  4. def main_demo() -> None:
  5. ui.markdown('This is **Markdown**.')
  6. @doc.demo('Markdown with indentation', '''
  7. Common indentation is automatically stripped from the beginning of each line.
  8. So you can indent markdown elements, and they will still be rendered correctly.
  9. ''')
  10. def markdown_with_indentation():
  11. ui.markdown('''
  12. ### Example
  13. This line is not indented.
  14. This block is indented.
  15. Thus it is rendered as source code.
  16. This is normal text again.
  17. ''')
  18. @doc.demo('Markdown with code blocks', '''
  19. You can use code blocks to show code examples.
  20. If you specify the language after the opening triple backticks, the code will be syntax highlighted.
  21. See [the Pygments website](https://pygments.org/languages/) for a list of supported languages.
  22. ''')
  23. def markdown_with_code_blocks():
  24. ui.markdown('''
  25. ```python
  26. from nicegui import ui
  27. ui.label('Hello World!')
  28. ui.run(dark=True)
  29. ```
  30. ''')
  31. @doc.demo('Markdown tables', '''
  32. By activating the "tables" extra, you can use Markdown tables.
  33. See the [markdown2 documentation](https://github.com/trentm/python-markdown2/wiki/Extras#implemented-extras) for a list of available extras.
  34. ''')
  35. def markdown_tables():
  36. ui.markdown('''
  37. | First name | Last name |
  38. | ---------- | --------- |
  39. | Max | Planck |
  40. | Marie | Curie |
  41. ''', extras=['tables'])
  42. @doc.demo('Mermaid diagrams', '''
  43. You can use Mermaid diagrams with the "mermaid" extra.
  44. See the [markdown2 documentation](https://github.com/trentm/python-markdown2/wiki/Extras#implemented-extras)
  45. for a list of available extras.
  46. ''')
  47. def mermaid():
  48. ui.markdown('''
  49. ```mermaid
  50. graph TD;
  51. A-->B;
  52. A-->C;
  53. B-->D;
  54. C-->D;
  55. ```
  56. ''', extras=['mermaid'])
  57. @doc.demo('LaTeX formulas', '''
  58. By activating the "latex" extra, you can use LaTeX formulas.
  59. This requires markdown2 version >=2.5 as well as latex2mathml to be installed.
  60. ''')
  61. def markdown_latex():
  62. ui.markdown(r'''
  63. Euler's identity:
  64. $$e^{i\pi} = -1$$
  65. ''', extras=['latex'])
  66. @doc.demo('Change Markdown content', '''
  67. You can change the content of a Markdown element by setting its `content` property or calling `set_content`.
  68. ''')
  69. def markdown_new_content():
  70. markdown = ui.markdown('Sample content')
  71. ui.button('Change Content', on_click=lambda: markdown.set_content('This is new content'))
  72. @doc.demo('Styling elements inside Markdown', '''
  73. To style HTML elements inside a `ui.markdown` element, you can add custom CSS rules for the "nicegui-markdown" class.
  74. ''')
  75. def markdown_styling():
  76. ui.add_css('''
  77. .nicegui-markdown a {
  78. color: orange;
  79. text-decoration: none;
  80. }
  81. .nicegui-markdown a:hover {
  82. color: orange;
  83. text-decoration: underline;
  84. }
  85. ''')
  86. ui.markdown('This is a [link](https://example.com).')
  87. doc.reference(ui.markdown)