demo_imgui_md.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. # Part of ImGui Bundle - MIT License - Copyright (c) 2022-2026 Pascal Thomet - https://github.com/pthom/imgui_bundle
  2. from imgui_bundle import imgui_md, immapp
  3. def example_markdown_string() -> str:
  4. markdown = r"""
  5. # Dear ImGui Bundle — Markdown tour
  6. `imgui_md` renders markdown directly inside an ImGui window — no browser,
  7. no HTML, no external renderer.
  8. > [!TIP]
  9. > Use this demo as a reference when writing markdown in your own application.
  10. > Expand the *"Show source"* sections to get copyable snippets.
  11. ---
  12. # Basics
  13. <details open>
  14. <summary>Text and typography</summary>
  15. All the usual inline styling works:
  16. - *emphasis*, **bold**, ***both***, ~~strikethrough~~, <u>underlined</u>
  17. - <mark>highlighted passages</mark> for things that need to stand out
  18. - Inline `code` — handy for tokens, flags, and short snippets
  19. HTML-like spans render natively too, no callbacks needed:
  20. - Keyboard shortcuts read naturally: press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd>, or <kbd>Cmd</kbd>+<kbd>K</kbd> on a Mac
  21. - Chemistry: H<sub>2</sub>O, CO<sub>2</sub>, C<sub>8</sub>H<sub>10</sub>N<sub>4</sub>O<sub>2</sub>
  22. - Exponents: x<sup>2</sup> + y<sup>2</sup> = r<sup>2</sup>
  23. For any HTML span not in the default set, wire `MarkdownCallbacks.on_html_span`.
  24. <details>
  25. <summary>Show source</summary>
  26. ```
  27. *emphasis*, **bold**, ***both***, ~~strikethrough~~, <u>underlined</u>
  28. <mark>highlighted</mark>, inline `code`
  29. <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd>
  30. H<sub>2</sub>O, x<sup>2</sup> + y<sup>2</sup> = r<sup>2</sup>
  31. ```
  32. </details>
  33. </details>
  34. <details>
  35. <summary>Structure: headers, lists, quotes, rules</summary>
  36. Markdown handles the bones of a document out of the box.
  37. **Headers** go from `#` (H1) to `###` (H3); all render as ImGui-styled headings.
  38. <details>
  39. <summary>Test Headers</summary>
  40. # Title 1
  41. A quick intro in normal text.
  42. ## Title 2
  43. ### Title 3
  44. #### Title 4
  45. ##### Title 5
  46. </details>
  47. **Ordered and unordered lists**, including nesting:
  48. 1. First
  49. 2. Second
  50. - Nested bullet
  51. - Another one
  52. 1. Deeper still
  53. 3. Third
  54. **Blockquotes** for callouts that aren't admonitions:
  55. > Markdown inside an ImGui window — the best of both worlds.
  56. **Horizontal rules** to separate sections (three dashes on a line):
  57. ---
  58. <details>
  59. <summary>Show source</summary>
  60. ```
  61. # H1
  62. ## H2
  63. ### H3
  64. 1. First
  65. 2. Second
  66. - Nested bullet
  67. 1. Deeper still
  68. > A blockquote.
  69. ---
  70. ```
  71. </details>
  72. </details>
  73. <details>
  74. <summary>Links and autolinks</summary>
  75. Explicit markdown link syntax: [Dear ImGui Bundle](https://github.com/pthom/imgui_bundle).
  76. Autolinks turn bare URLs, `www.` hosts, and email addresses into clickable
  77. links automatically:
  78. - https://github.com/pthom/imgui_bundle
  79. - www.dearimgui.org
  80. - Contact: pthomet@gmail.com
  81. Disable with `MarkdownOptions.autolinks = False` for strict CommonMark
  82. behavior.
  83. <details>
  84. <summary>Show source</summary>
  85. ```
  86. [Dear ImGui Bundle](https://github.com/pthom/imgui_bundle)
  87. https://github.com/pthom/imgui_bundle
  88. www.dearimgui.org
  89. Contact: pthomet@gmail.com
  90. ```
  91. </details>
  92. </details>
  93. <details>
  94. <summary>Images</summary>
  95. Images load from local assets with a relative path:
  96. ![World](images/world.png)
  97. Remote URLs load asynchronously — a spinner is shown while downloading:
  98. ![Photo](https://picsum.photos/id/1018/300/200)
  99. Use `<img>` when you need to control the size:
  100. <img src="https://picsum.photos/id/237/300/200" width="100">
  101. <details>
  102. <summary>Show source</summary>
  103. ```
  104. ![World](images/world.png)
  105. ![Photo](https://picsum.photos/id/1018/300/200)
  106. <img src="https://picsum.photos/id/237/300/200" width="100">
  107. ```
  108. </details>
  109. </details>
  110. # Tables and code blocks
  111. <details>
  112. <summary>Code blocks</summary>
  113. **Inline snippets**
  114. Inline snippets are rendered like code inside a regular paragraph, like this: `result = 37`.
  115. They are written like this:
  116. <pre>
  117. `result = 37`
  118. </pre>
  119. **Code blocks**
  120. Code blocks preserve layout and use a monospaced font.
  121. A small Python example:
  122. ```python
  123. from imgui_bundle import imgui, immapp
  124. def gui():
  125. imgui.text("Hello, World!")
  126. immapp.run(gui, window_title="My App")
  127. ```
  128. And its C++ equivalent:
  129. ```cpp
  130. #include "imgui.h"
  131. #include "immapp/immapp.h"
  132. void gui() {
  133. ImGui::Text("Hello, World!");
  134. }
  135. int main() {
  136. ImmApp::Run(gui);
  137. }
  138. ```
  139. Code blocks are delimited by three backticks, plus an optional language. See example below:
  140. <pre>
  141. ```python
  142. int main()
  143. {
  144. return 0;
  145. }
  146. ```
  147. </pre>
  148. </details>
  149. <details>
  150. <summary>Tables</summary>
  151. Columns are resizable at runtime — grab a column border and drag. First-row
  152. widths drive column layout, so use `&nbsp;` in that row to enforce a
  153. minimum width where needed.
  154. Column alignment is controlled by colons in the separator row:
  155. ```
  156. :--- left
  157. ---: right
  158. :---: centre
  159. ```
  160. | Continent | Population | Countries |
  161. |----------------|--------------:|:---------:|
  162. | Africa | 1300 million | 54 |
  163. | Asia | 4500 million | 48 |
  164. | Europe | 743 million | 44 |
  165. | North America | 579 million | 23 |
  166. | Oceania | 41 million | 14 |
  167. | South America | 422 million | 12 |
  168. | Antarctica | 0 | 0 |
  169. <details>
  170. <summary>Show source</summary>
  171. ```
  172. | Continent | Population | Countries |
  173. |----------------|--------------:|:---------:|
  174. | Africa | 1300 million | 54 |
  175. | Asia | 4500 million | 48 |
  176. ```
  177. </details>
  178. </details>
  179. # Useful extensions
  180. <details>
  181. <summary>GitHub-style admonitions</summary>
  182. Blockquotes that start with `[!NOTE]`, `[!TIP]`, `[!IMPORTANT]`, `[!WARNING]`
  183. or `[!CAUTION]` render as coloured callouts — great for in-app help and
  184. onboarding:
  185. > [!NOTE]
  186. > A note provides useful context that a reader should know.
  187. > [!TIP]
  188. > A small hint that saves the reader time.
  189. > [!IMPORTANT]
  190. > Required information to complete a task.
  191. > [!WARNING]
  192. > Heads up: something can subtly go wrong here.
  193. > [!CAUTION]
  194. > A negative outcome is likely without care.
  195. <details>
  196. <summary>Show source</summary>
  197. ```
  198. > [!NOTE]
  199. > A note provides useful context that a reader should know.
  200. > [!WARNING]
  201. > Heads up: something can subtly go wrong here.
  202. ```
  203. </details>
  204. </details>
  205. <details>
  206. <summary>Task lists</summary>
  207. GitHub-style task lists render as checkbox glyphs. Handy for changelogs
  208. and roadmap-style content embedded inside your app:
  209. - [x] Design the UI
  210. - [x] Wire up the data layer
  211. - [x] Write the onboarding flow
  212. - [ ] Polish documentation
  213. - [ ] Record a demo video
  214. - [ ] Ship it
  215. <details>
  216. <summary>Show source</summary>
  217. ```
  218. - [x] Done item
  219. - [ ] Pending item
  220. ```
  221. </details>
  222. </details>
  223. <details>
  224. <summary>Math with LaTeX</summary>
  225. Requires `immapp.run(..., with_latex=True)`. Rendering is powered by
  226. [MicroTeX](https://github.com/NanoMichael/MicroTeX).
  227. **Inline math** uses single dollars: Euler's identity $e^{i\pi} + 1 = 0$
  228. is a consequence of the more general $e^{i\theta} = \cos\theta + i\sin\theta$.
  229. **Display math** uses double dollars on their own line. The quadratic
  230. formula:
  231. $$
  232. x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
  233. $$
  234. Sums, integrals and matrices all work:
  235. $$
  236. \int_{-\infty}^{\infty} e^{-x^2}\, dx = \sqrt{\pi}
  237. \qquad
  238. A = \begin{pmatrix} a & b \\ c & d \end{pmatrix}
  239. \qquad
  240. \sum_{i=0}^{n} i = \frac{n(n+1)}{2}
  241. $$
  242. <details>
  243. <summary>Show source</summary>
  244. ```
  245. Inline: $e^{i\pi} + 1 = 0$
  246. Display:
  247. $$
  248. x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
  249. $$
  250. ```
  251. </details>
  252. </details>
  253. <details>
  254. <summary>Preformatted text with the pre tag</summary>
  255. `<pre>` renders a block of monospaced text **without** the styling of a
  256. fenced code block — no background frame, no syntax coloring. Use it
  257. for ASCII layouts, aligned data, and anything where "monospace" is
  258. what you want but "this is source code" is not the right message:
  259. <pre>
  260. Metric Aligned Value
  261. -------- --------- -----
  262. Ping right 12ms
  263. Throughput right 42MB/s
  264. Latency right 87us
  265. </pre>
  266. Same content in a fenced code block, for comparison:
  267. ```
  268. Metric Aligned Value
  269. -------- --------- -----
  270. Ping right 12ms
  271. ```
  272. <details>
  273. <summary>Show source</summary>
  274. ```
  275. <pre>
  276. First line
  277. Indented line
  278. Last line
  279. </pre>
  280. ```
  281. </details>
  282. </details>
  283. ---
  284. # Under the hood: how this page is built
  285. <details>
  286. <summary>It's collapsibles all the way down</summary>
  287. Every section above — and every "Show source" inside them — is a
  288. `<details>` block. The tags render as `CollapsingHeader` widgets,
  289. and blank lines around the opening / closing tags let the inner
  290. content be parsed as regular markdown:
  291. <details>
  292. <summary>A nested collapsible</summary>
  293. Hidden until you click. The content is regular markdown.
  294. - One
  295. - Two
  296. <details>
  297. <summary>Going deeper</summary>
  298. Another level. Indentation doesn't matter; what matters is the blank
  299. lines around the tags.
  300. </details>
  301. </details>
  302. <details>
  303. <summary>Show source</summary>
  304. ```
  305. <details>
  306. <summary>Click me</summary>
  307. Hidden content (regular markdown here).
  308. </details>
  309. ```
  310. </details>
  311. </details>
  312. """
  313. return markdown
  314. def demo_gui():
  315. # from imgui_bundle import hello_imgui
  316. # hello_imgui.apply_theme(hello_imgui.ImGuiTheme_.white_is_white)
  317. s = example_markdown_string()
  318. imgui_md.render(s)
  319. # Note: you may also use:
  320. # imgui_md.render_unindented(s)
  321. # (it will remove the main indentation of the Markdown string before rendering it,
  322. # which is useful when the string is defined inside a function with indentation)
  323. def main():
  324. immapp.run(demo_gui, with_latex=True, window_size=(800, 800))
  325. if __name__ == "__main__":
  326. main()