I realize this is an old question so it may not help too much with your webbook, but for future people coming across this question there are now a few ways to run Python in the browser.
Pyodide is a great packages which runs a Python interpreter inside of the browser using WebAssembly.
PyScript is a project built on top of Pyodide which allows running python using the <pyscript> tag. However, it is very slow to load and doesn't provide a great interfaces for editing and demo-ing Python.
This is where PyPrez comes in (Disclaimer: I am the author). PyPrez is built to allow you to easily embed runnable and editable codeblocks in the browser. It is tailor made for use cases such as interactive Python tutorials or webbooks like what you describe. In fact, it even works in StackOverflow answers if you click Run Code Snippet
# python example
class Car(object):
num_wheels = 4
mpg = 20
gas_capacity = 15
class NotEnoughGasError(Exception):
pass
def __init__(self):
self.odometer = 0
self.gas = self.gas_capacity
def drive(self, miles):
gallons_needed = miles / self.mpg
if not self.gas > gallons_needed:
raise self.NotEnoughGasError(f"{miles}mi trip requires {gallons_needed} gallons of gas but there are only {self.gas} gallons left")
print(f"driving {miles} miles")
self.odometer += miles
self.gas -= gallons_needed
def fill_gas(self):
self.gas = self.gas_capacity
if __name__ == "__main__":
my_car = Car()
my_car.drive(200)
my_car.drive(50)
my_car.drive(75)
<script src="https://modularizer.github.io/pyprez/pyprez.min.js"></script>
print('hello world!').