In this lecture we will learn how to put in place a program with Brython, a client side web programming tool. Brython is a JavaScript library that interprets Python code, but not only. Its goals are mainly:
Python is more fun to write and is more readable than JavaScript. However the JavaScript specification is still evolving and it is part of a very large growing ecosystem (asm, dart (see also a comparison between Dart and JavaScript), typescript). Note that there exists many projects for Python-to-JavaScript compilers:
A client side web application means that you download an html page containing all the information to compute and to display the result of your problem. There is no server, except a repository for your html file. Your browser interprets the code inside the html file, and solve the problem!
The magic part is that the html file is divided into (virtual) sections of code as follows:
This type of organization is known under the vocabulary of MVC (Model View Controller). It is placed at the methodological level regarding the development of applications and may be useful for developing database applications (MySQL, MariaDB, Postgresql...) or GUI (Graphical User Interface) with JavaFX and Swing (Java tools), or in many other situations. The following frameworks also use an MVC approach: Angular (becoming very popular), Backbone, Ember, Knockout. Some complementary explanations about MVC can be found on this page.
We recommend to download all the examples and to inspect the codes. We put some comments inside the codes in order to discuss the limitations of Brython. The portions of codes with a comment (# is used in Python to introduce a comment) show alternatives that are theoretically possible but, in general, not implemented yet with Brython (for good and maybe for bad reasons too).
The reader of this lecture is now invited to review, in depth, the codes inside the following three examples in order to understand them according to the MVC principles:
requests
interface, for all the cities, and to
store locally the results, on your local disk. The main loop is:
for i,_ in dico.items(): print('We are creating the file named ',dico[i]+'_fr.xml -- Please wait!') data = requests.get('http://worldweather.wmo.int/fr/json/'+dico[i]+'_fr.xml') dataa=data.text j = json.loads(dataa) # Open a file fo = open(dico[i]+'_fr.xml', "w") foo = str(j) foo=foo.replace(": None", ": 'None'") foo=foo.replace(": False", ": 'False'") foo=foo.replace(": True", ": 'True'") # we put the anchorTo conclude, we obtain two independant programs, that do not really cooperate (only through files). The Python program generates the data for the HTML code. The communication between the HTML code and the 'outside' of the browser is accomplished with an ajax POST request. The Python program needs to be started daily if you want fresh information about the weather!in order to pre-build # an XML object that will be read by an ajax request in the # weather-station.html file. This is a Tip! fo.write(" "+foo+" ") # Close opend file fo.close()
Use a NodeJS server to implement the dialog with your HTML/python application regarding the POST requests over the Internet that is necessary to download information from http://worldweather.wmo.int
Use a Redis server to implement the dialog with your HTML/python applicationregarding the POST requests over the Internet that is necessary to download information from http://worldweather.wmo.int
Special note: the Brython web site contains also many demos that can be considered to capture the main facilities to implement user interfaces.
Special note: the following post contains interesting information about client side web programming, notably a general discussion of issues, technical solution for implementing data science librairy inside the browser i.e. Bringing the scientific Python stack to the browser according to the viion of the Pyodide project. Pyodide is the Python scientific stack, compiled to WebAssembly. WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is a core technology for Iodide. Iodide is a modern, literate, and interactive programming environment that uses the strengths of the browser to let scientists work flexibly and collaboratively with minimal friction. In the same vein, the Nexedi team uses all of these technologies for the OfficeJS Iodide project, which is an ubiquitous Data Science Notebooks for Business and Education, running in the browser.