IntroductionInstallationLimitations of the "file" protocolFrequently asked questionsSyntax, keywords and built-in functionsStandard distributionimport implementationBrython packagesBrowser interface
Introduction - DOM API
Creating a document Accessing elements Attributes, properties and methods Events Mouse events Keyboard events Focus events Drag events Query string Interactions with Javascript Brython-specific built-in modules
browser
browser.aio browser.ajax browser.html browser.local_storage browser.markdown browser.object_storage browser.session_storage browser.svg browser.template browser.timer browser.webcomponent browser.websocket browser.worker Widgets browser.widgets.dialog browser.widgets.menu interpreter javascript Working with BrythonCookbook |
Implementation of importLike in standard Python, you can install modules or packages Python in your application by putting them in the root directory, or in directories with a file __init__.py. Note that modules must be encoded in utf-8 ; the encoding declaration at the top of the script is ignored. For instance, the application can be made of the following files and directories :.bundle-include app.html brython.js brython_modules.js brython_stdlib.js index.html users.py utils.py + app __init__.py records.py tables.pyA Python script in app.html can run the imports import users import app.recordsIf the standard distribution has been included in the page by <script type="text/javascript" src="brython_stdlib.js"></script>the script can also run import datetime import reTo import modules or packages, Brython uses the same mechanism as CPython : to resolve "import X", the program looks for a file in several places :
id :
<script type="text/python" id="module"> def hello(): return "world" </script> <script type="text/python"> from browser import document import module document.body <= module.hello() </script> OptimisationThe process described above has two main drawbacks :
pip , you can generate
a file brython_modules.js which only holds the modules used by the
application.
For that, open a console window, navigate to the application directory and
execute
brython-cli make_modulesNote that this program parses the Brython code in all the scripts, modules and HTML pages of the directory and its sub-directories. The CPython version used must be compliant with this Brython code : for instance if there are match / case in the Brython code, CPython 3.10+ is required, otherwise you
would get syntax errors.
You can then replace all the occurrences of
<script type="text/javascript" src="brython_stdlib.js"></script> by <script type="text/javascript" src="brython_modules.js"></script> |