Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
| Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung Nächste Überarbeitung | Vorherige Überarbeitung | ||
| user:hof:pyeditor [2026-04-13 11:32] – [Exercises] hof | user:hof:pyeditor [2026-04-24 14:29] (aktuell) – hof | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| # Bottom Python Editor | # Bottom Python Editor | ||
| - | < | ||
| Bottom Editor offers a standalone editor version at [[https:// | Bottom Editor offers a standalone editor version at [[https:// | ||
| Also see [[~kara]] for a Kara shim on top of bottom-editor. | Also see [[~kara]] for a Kara shim on top of bottom-editor. | ||
| - | |||
| ## Basic Usage | ## Basic Usage | ||
| + | |||
| + | ### Direct HTML | ||
| + | Requires HTML-authoring (which may be a security risk with broad authorship). | ||
| Within Dokuwiki, the HTML needs to be embedded into `< | Within Dokuwiki, the HTML needs to be embedded into `< | ||
| Zeile 20: | Zeile 21: | ||
| </ | </ | ||
| </ | </ | ||
| + | |||
| + | ### With Plugin | ||
| + | To avoid needing raw trusted HTML access, install the [[https:// | ||
| + | |||
| + | <code html> | ||
| + | < | ||
| + | print(42) | ||
| + | </ | ||
| + | </ | ||
| + | |||
| ## Shared Sessions | ## Shared Sessions | ||
| By default, all editors on a page share a single python runtime. Functions defined in one execution are available in all editors afterwards, similar to jupyter notebooks. If you want an editor to have a separate python runtime, use the `session` attribute: | By default, all editors on a page share a single python runtime. Functions defined in one execution are available in all editors afterwards, similar to jupyter notebooks. If you want an editor to have a separate python runtime, use the `session` attribute: | ||
| <code html> | <code html> | ||
| - | < | + | < |
| def greet(name): | def greet(name): | ||
| print(f' | print(f' | ||
| - | </ | + | </ |
| - | < | + | < |
| greet(' | greet(' | ||
| - | </ | + | </ |
| - | < | + | < |
| greet(' | greet(' | ||
| - | </ | + | </ |
| </ | </ | ||
| - | < | + | < |
| def greet(name): | def greet(name): | ||
| print(f' | print(f' | ||
| - | </ | + | </ |
| - | < | + | < |
| greet(' | greet(' | ||
| - | </ | + | </ |
| - | + | < | |
| - | < | + | |
| greet(' | greet(' | ||
| - | </ | + | </ |
| Zeile 57: | Zeile 67: | ||
| <code html> | <code html> | ||
| - | < | + | < |
| import turtle | import turtle | ||
| Zeile 67: | Zeile 77: | ||
| t.forward(i * 0.5) | t.forward(i * 0.5) | ||
| t.left(59) | t.left(59) | ||
| - | </ | + | </ |
| </ | </ | ||
| - | < | + | < |
| import turtle | import turtle | ||
| Zeile 80: | Zeile 90: | ||
| t.forward(i * 0.5) | t.forward(i * 0.5) | ||
| t.left(59) | t.left(59) | ||
| - | </ | + | </ |
| ## Matplotlib | ## Matplotlib | ||
| Zeile 86: | Zeile 96: | ||
| <code html> | <code html> | ||
| - | < | + | < |
| # Load packages from within python using micropip | # Load packages from within python using micropip | ||
| import micropip | import micropip | ||
| Zeile 102: | Zeile 112: | ||
| plt.tight_layout() | plt.tight_layout() | ||
| plt.show() | plt.show() | ||
| - | </ | + | </ |
| </ | </ | ||
| - | < | + | < |
| # Load packages from within python using micropip | # Load packages from within python using micropip | ||
| import micropip | import micropip | ||
| Zeile 121: | Zeile 131: | ||
| plt.tight_layout() | plt.tight_layout() | ||
| plt.show() | plt.show() | ||
| - | </ | + | </ |
| ## OpenCV | ## OpenCV | ||
| Zeile 127: | Zeile 137: | ||
| <code html> | <code html> | ||
| - | < | + | < |
| # Load packages from within python using micropip | # Load packages from within python using micropip | ||
| import micropip | import micropip | ||
| Zeile 144: | Zeile 154: | ||
| cv2.putText(img, | cv2.putText(img, | ||
| cv2.imshow(' | cv2.imshow(' | ||
| - | </ | + | </ |
| </ | </ | ||
| - | < | + | < |
| # Load packages from within python using micropip | # Load packages from within python using micropip | ||
| import micropip | import micropip | ||
| Zeile 164: | Zeile 174: | ||
| cv2.putText(img, | cv2.putText(img, | ||
| cv2.imshow(' | cv2.imshow(' | ||
| - | </ | + | </ |
| ## Installing files | ## Installing files | ||
| Zeile 170: | Zeile 180: | ||
| <code html> | <code html> | ||
| - | < | + | < |
| with open(' | with open(' | ||
| for line in infile: | for line in infile: | ||
| Zeile 177: | Zeile 187: | ||
| if town == ' | if town == ' | ||
| print(f' | print(f' | ||
| - | </ | + | </ |
| </ | </ | ||
| - | < | + | < |
| with open(' | with open(' | ||
| for line in infile: | for line in infile: | ||
| Zeile 187: | Zeile 197: | ||
| if town == ' | if town == ' | ||
| print(f' | print(f' | ||
| - | </ | + | </ |
| ## Exercises | ## Exercises | ||
| <code html> | <code html> | ||
| - | <html>< | + | < |
| - | < | + | Write a function < |
| - | | + | |
| <tt>1 + 2 + & | <tt>1 + 2 + & | ||
| - | <tt>n ≤ 0</ | + | <tt>n ≤ 0</ |
| <script type=" | <script type=" | ||
| def sum_to(n): | def sum_to(n): | ||
| Zeile 205: | Zeile 215: | ||
| assert sum_to(0) == 0, " | assert sum_to(0) == 0, " | ||
| </ | </ | ||
| - | </bottom-exercise></html> | + | |
| + | def sum_to(n): | ||
| + | return sum(range(n+1)) | ||
| + | | ||
| + | </ | ||
| </ | </ | ||
| - | < | + | < |
| - | <div slot="prompt"> | + | Write a function <tt> |
| - | | + | <tt>1 + 2 + & |
| - | <tt>1 + 2 + & | + | <tt>n ≤ 0</ |
| - | <tt>n ≤ 0</ | + | <script |
| - | </div> | + | |
| - | < | + | |
| def sum_to(n): | def sum_to(n): | ||
| pass | pass | ||
| - | </template> | + | </script> |
| - | <template data-type=" | + | <script |
| assert sum_to(5) == 15, " | assert sum_to(5) == 15, " | ||
| assert sum_to(1) == 1, " | assert sum_to(1) == 1, " | ||
| assert sum_to(0) == 0, " | assert sum_to(0) == 0, " | ||
| - | </template> | + | </script> |
| - | </ | + | <script type=" |
| + | def sum_to(n): | ||
| + | | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ## Persistence | ||
| + | |||
| + | Editors or excercises with an `id` attribute store the current state in the browser' | ||
| + | |||
| + | <code html> | ||
| + | < | ||
| + | print(42) | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | |||
| + | < | ||
| + | print(42) | ||
| + | </ | ||