# Bottom Python Editor
Bottom Editor offers a standalone editor version at [[https://bottom.ch/editor/]] and a web component embeddable as normal HTML elements after loading the module once. See the [[https://bottom.ch/editor/stable/doc/|public documentation]] for all options.
Also see [[~kara]] for a Kara shim on top of bottom-editor.
## Basic Usage
Within Dokuwiki, the HTML needs to be embedded into `` tags for verbatim HTML.
print(42)
## 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:
def greet(name):
print(f'Hi, {name}!')
greet('Harry') # works
greet('Harry') # fails
def greet(name):
print(f'Hi, {name}!')
greet('Harry') # works
greet('Harry') # fails
## Turtle
Standard Python turtle with a small canvas. Use `layout="split"` to have both canvas and console.
import turtle
t = turtle.Turtle()
t.speed(9)
colors = ['red', 'orange', 'gold', 'green', 'blue', 'purple']
for i in range(90):
t.pencolor(colors[i % len(colors)])
t.forward(i * 0.5)
t.left(59)
import turtle
t = turtle.Turtle()
t.speed(9)
colors = ['red', 'orange', 'gold', 'green', 'blue', 'purple']
for i in range(90):
t.pencolor(colors[i % len(colors)])
t.forward(i * 0.5)
t.left(59)
## Matplotlib
`plt.show()` renders to the canvas.
# Load packages from within python using micropip
import micropip
await micropip.install('matplotlib')
import matplotlib.pyplot as plt
import math
x = [i * 0.1 for i in range(63)]
plt.figure(figsize=(5, 4))
plt.plot(x, [math.sin(v) for v in x], label='sin')
plt.plot(x, [math.cos(v) for v in x], label='cos')
plt.legend()
plt.title('Trigonometric functions')
plt.tight_layout()
plt.show()
# Load packages from within python using micropip
import micropip
await micropip.install('matplotlib')
import matplotlib.pyplot as plt
import math
x = [i * 0.1 for i in range(63)]
plt.figure(figsize=(5, 4))
plt.plot(x, [math.sin(v) for v in x], label='sin')
plt.plot(x, [math.cos(v) for v in x], label='cos')
plt.legend()
plt.title('Trigonometric functions')
plt.tight_layout()
plt.show()
## OpenCV
`cv2.imshow()` renders to the canvas.
# Load packages from within python using micropip
import micropip
await micropip.install('numpy')
await micropip.install('opencv-python')
import numpy as np
import cv2
# Gradient image with a circle
img = np.zeros((300, 300, 3), dtype=np.uint8)
for y in range(300):
for x in range(300):
img[y, x] = [x * 255 // 300, y * 255 // 300, 128]
cv2.circle(img, (150, 150), 80, (255, 255, 255), 3)
cv2.putText(img, 'OpenCV', (75, 160), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)
cv2.imshow('result', img)
# Load packages from within python using micropip
import micropip
await micropip.install('numpy')
await micropip.install('opencv-python')
import numpy as np
import cv2
# Gradient image with a circle
img = np.zeros((300, 300, 3), dtype=np.uint8)
for y in range(300):
for x in range(300):
img[y, x] = [x * 255 // 300, y * 255 // 300, 128]
cv2.circle(img, (150, 150), 80, (255, 255, 255), 3)
cv2.putText(img, 'OpenCV', (75, 160), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)
cv2.imshow('result', img)
## Installing files
You can install files from an URL (with CORS headers if from a different domain!):
with open('gemeinden.csv', 'r') as infile:
for line in infile:
tokens = line.split(',')
town = tokens[0]
if town == 'Romanshorn':
print(f'Romanshorn hat {tokens[2]} Einwohner')
with open('gemeinden.csv', 'r') as infile:
for line in infile:
tokens = line.split(',')
town = tokens[0]
if town == 'Romanshorn':
print(f'Romanshorn hat {tokens[2]} Einwohner')
## Exercises
Write a function sum_to(n) that returns
1 + 2 + … + n. Return 0 for
n ≤ 0.
Write a function sum_to(n) that returns
1 + 2 + … + n. Return 0 for
n ≤ 0.