How Brython Works
For understanding how Brython works, we first have to install brython with our preferred method, which we have discussed in "How to install Brython article".
Few this, we should know before discussing how brython works:
In this article, we will discuss Brython core components, standard libraries, and how it works.
Brython Core Components
The brython.js and brython.min.js (the minimized version of the Brython engine) contains the core components of brython. Both of them contains the following key components:
The brython.js and brython.min.js (the minimized version of the Brython engine) contain the core components of Brython. Both of them contains the following key components:
Brython Standard Library
After having an idea of what core Brython file, brython.js, we will now learn about their companion file, brython_stdlib.js.
Brython should be closed to CPython, which the Python's preferred implementation. As Brython runs inside the context of a website browser, it has few limitations, such as the browser does not allow direct access to the file system, so, os.open() function cannot open the file. Functions which are not related to the website browser cannot be implemented.
# let's run the code in the Brython environment:
TypeError Traceback (most recent call last) <ipython-input-1-85e82a7fc62f> in <module> 1 import os ----> 2 os.unlink() TypeError: unlink() missing required argument 'path' (pos 1)
Here, os.unlink() is raising an exception, as it is not secure for deletion of a local file from the browser environment, and the File and Directories Entries API is just a default proposal.
The user needs to include brython_stdlib.js and brython_stdlib.min.js for importing modules from the Python standard library.
After loading and parsing the HTML page, brython() will take these following steps:
# embed the Python code in the HTML file:
Second option is to download the Python program from the separate file:
In the above case the Python file will look like this:
Internals of Brython
For illustrating the process, and see what is the internals of Brython, the user should use the following step:
In this article, we have discussed and explain the core components and standard libraries of Brython and how the Brython works on the Browser. We have also talked about the internals of Brython and how it operated the websites from the background.