infinite quanta, inc., a 501(c)(3) NonProfit Scientific Research Organization Home Page The Peptide Reseach Project Our Organization Our Associates Collaboration News & Events [ Log In | Sign Up | Lost password? ]

web2py on the Raspberry

Sunday, February 14, 2021 - web2py is a server-side web server designed for processing database and web requests. Its entirely written in the python language. In fact, this very website has been served with web2py for the past 12 years without flaw or crash. It will be used to develope the graphical-user interface of the instrument and allow for real-time event-driven monitoring of the experiment during its operation.


web2py is analogous to Server-side Java (SSJ). It resides on a web server and responses to client-side web requests. Some of those responses can be simple static web pages, like a home page to mom and pops cake-making business in your neighborhood. The real power of the internet came when developers learned to respond with dynamic content. That is content tailored to the client-side requests, the user. Dynamic content usually allows users to login to the site and grant special access and respond with user-specific content, like when a student is given their own special test so they can't "collaborate" with their classmates. Dynamic content usually employs databases to store user states to dole out that user-specific content. web2py uses python as its development platform and programming language to act as that middle tier on the server between the database and responding to the client-side user requests. web2py's web site is located at: web2py Web Framework.

Installing web2py is fairly straight-forward and begin with:

> mkdir -p /opt/web2py_apps
> adduser -d /opt/web2py_apps web2py
> cd /opt/web2py_apps
> wget http://web2py.com/examples/static/web2py_src.zip
> unzip web2py_src.zip
> cat web2py/VERSION
Version 2.21.1-stable+timestamp.2020.11.28.04.10.44
> mv web2py web2py_2.21.1
> ln -s web2py_2.21.1/ web2py
> cd web2py

> nano anyserver.py
#put a 3 on the end of python in the first line
#!/usr/bin/env python3
>> Ctrl-X, Save? >> Y, Enter

> nano web2py_start.sh
#!/bin/bash
kill $(ps aux | grep '[p]ython' | awk '{print $2}')
sleep 3
/opt/web2py_apps/web2py/anyserver.py -i 0.0.0.0 -p 80 &
exit 0
>> Ctrl-X, Save? >> Y, Enter

> nano web2py_stop.sh
#!/bin/bash
kill $(ps aux | grep '[p]ython' | awk '{print $2}')
exit 0
>> Ctrl-X, Save? >> Y, Enter

> chmod 775 anyserver.py web2py_start.sh web2py_stop.sh

> nano /etc/rc.local
#added above exit 0
/opt/web2py_apps/web2py/web2py_start.sh &
>> Ctrl-X, Save? >> Y, Enter

> python -c "from gluon.main import save_password; save_password(raw_input('admin password: '),443)"
> chown -R web2py:web2py /opt/web2py_apps/
> /opt/web2py_apps/web2py/web2py_start.sh &
starting rocket on 0.0.0.0:80...
> ps -A | grep python
 5313 pts/0    00:00:01 python3

where copying the web2py subdirectory to a version-based subdirectory allows for incremental updating of web2py in the future. When that updating occurs, just need to recreate the symbolic link of web2py to the new version subdirectory.

Now that web2py is up and running. It is required to install other python libraries to bring a more visual and graphical experience to the instrument interface. web2py will need access to graphing modules and client-side java-script.

Java-script is natively run on the client-side and is the inherent language of all web browsers. So, essentially, when algorithms need to run on the user's computer, under their browser, java-script is used. Yes, its understandable that you have understand and program in about 12 languages today, but that is the world of development these days. jQuery is a good solid system for effectively using java-script across every operating-system and browser. jQuery will be installed on the server, by web2py, and sent to the client so the browser can access it.

> pip3 install --upgrade matplotlib mpld3
> cd /opt/web2py_apps/web2py/applications/instrument/static/js/
> ln -s /usr/local/lib/python3.7/dist-packages/mpld3/js/d3.v* .
> ln -s /usr/local/lib/python3.7/dist-packages/mpld3/js/mpld3.v0.5.2.* .

where "mpld3: A D3 Viewer for Matplotlib" is a lite-weight matplotlib to java-script converter that allows matplotlib's graphs to be interactive within a client side web page. It's also necessary to include the ~~~ src="paths_and_filename" type="text/javascript" charset="utf-8"> tags in the header of the html output to the d3.v5.min.js and mpld3.v0.5.2.js files so that those two libraries are available to the web browser for proper graph display and interaction.

<script src="/path_to_this_file/d3.v5.min.js" type="text/javascript" charset="utf-8"></script>
<script src="/path_to_this_file/mpld3.v0.5.2.js" type="text/javascript" charset="utf-8"></script>





Please Register / Login here to post or edit comments or questions to our blog.

Back to the Main Blog Page