from IPython.display import HTML, Image
HTML('<iframe src=https://www.destroyallsoftware.com/talks/wat width=900 height=500></iframe>')
HTML('<iframe src=http://altjs.org/ width=900 height=350></iframe>')
from IPython.display import HTML
HTML('<iframe src=http://stromberg.dnsalias.org/~strombrg/pybrowser/python-browser.html width=900 height=350></iframe>')
by Btrot69
a = 1
b = 2
print(a + b)
a, b = 1, 2
print(a + b)
print(type(a))
print(type(a * 1.0))
print(1 + 'a')
HTML('<iframe src=http://brython.info/tests/console.html width=900 height=350></iframe>')
def sum(a, b):
return a + b
print(sum(2,2))
HTML('<iframe src=http://brython.info/tests/console.html width=900 height=350></iframe>')
class Square:
def __init__(self, length):
self.len = length
def area(self):
return self.len ** 2
sq = Square(2)
print(sq.area())
HTML('<iframe src=http://brython.info/tests/console.html width=900 height=350></iframe>')
class Square:
def __init__(self, length):
self.len = length
def area(self):
return self.len ** 2
sq = Square(2)
print(sq.area())
class Square2(Square):
def perimeter(self):
return self.len * 4
sq = Square2(10)
print(sq.area())
print(sq.perimeter())
HTML('<iframe src=http://brython.info/tests/console.html width=900 height=350></iframe>')
Python classes (including built-in classes) are implemented with 2 different Javascript objects : a factory function that creates instances (it uses __new__
and __init__
when available) and a dictionary that holds the class attributes and methods.
(*) super
is not implemented in Brython 1.2 (well, it is since some days ago).
def world(some_func):
def pre(arg):
greet = some_func(arg)
return greet + ' world!'
return pre
@world
def whatever(word):
return word
print(whatever('hello'))
HTML('<iframe src=http://brython.info/tests/console.html width=900 height=350></iframe>')
as, assert, break, class, continue, def, del, elif, else, except, False, finally, for, from, global, if, import, is, lambda, None, pass, return, True, try, while, with, yield
abs(), all(), any(), ascii(), bin(), bool(), bytes(), callable(), chr(), classmethod(), delattr(), dict(), dir(), divmod(), enumerate(), eval(), exec(), filter(), float(), frozenset(), getattr(), globals(), hasattr(), hash(), hex(), id(), input(), int(), isinstance(), iter(), len(), list(), locals(), map(), max(), min(), next(), object(), open(), ord(), pow(), print(), property(), range(), repr(), reversed(), round(), set(), setattr(), slice(), sorted(), str(), sum(), tuple(), type(), zip()
<script type="text/javascript" src="path/to/the/library/brython.js">
body
tag¶<body onload="brython()">
script
tag using text/python
or text/python3
¶<script type="text/python">
...Your Python code...
</script>
print()
will output to the web browser console and so are the error messages. sys.stderr
and sys.stdout
can be assigned to an object with a write()
method, and this allows for the redirection of output to go to a window or text area, for example.¶sys.stdin
is not implemented at this time, however there is an input()
built-in function that will open a blocking input dialog (a prompt).¶win.print()
.¶built-ins
alert(), confirm(), prompt()
win
keyword is the window (window object in JS) and doc
represents the HTML document (document in JS).¶HTML('<iframe src=http://curious-electric.com/brython-playground/ width=900 height=400></iframe>')
<input id="data">
data = doc["data"]
import html
links = doc[html.A]
elt.get(name=N) returns a list of all the elements descending from elt whose attribute name is equal to N
elt.get(selector=S) returns a list with all the elements descending from elt whose CSS selector matches S
HTML('<iframe src=https://googledrive.com/host/0B4OEtv-kAaTBSllJM19hdkpCeTQ/colors_bootstrapped.html width=900 height=350></iframe>')
HTML('<iframe src=https://googledrive.com/host/0B4OEtv-kAaTBSllJM19hdkpCeTQ/table_bootstrapped.html width=900 height=350></iframe>')
HTML('<iframe src=https://googledrive.com/host/0B4OEtv-kAaTBSllJM19hdkpCeTQ/puzzle_bootstrapped.html width=900 height=350></iframe>')
HTML('<iframe src=https://googledrive.com/host/0B4OEtv-kAaTBSllJM19hdkpCeTQ/jsonp_bootstrapped.html width=900 height=350></iframe>')
HTML('<iframe src=https://googledrive.com/host/0B4OEtv-kAaTBSllJM19hdkpCeTQ/hangman_bootstrapped.html width=900 height=500></iframe>')
HTML('<iframe src=https://googledrive.com/host/0B4OEtv-kAaTBSllJM19hdkpCeTQ/todo_bootstrapped.html width=900 height=500></iframe>')
Image("http://images.fanpop.com/images/image_uploads/sponge-bob-rocks-spongebob-squarepants-154588_427_600.jpg")