Getting started with Python on the Rascal

The Rascal ships with a built-in webserver. It serves HTML and images like a normal webserver, but it can also execute Python code in response to web requests.

The file server.py contains all the functions that control how the Rascal reacts to requests from the web. The functions are organized by URL, and you can add your own functions to make the Rascal react to a new URL.

A minimal example

Here's the basic structure of a function in server.py.

@public.route('/path', methods=['POST'])
def function_name():
do_something()
return render_template('/page.html')

Here's what a function might look like filled in.

@public.route('/send-to-lcd', methods=['POST'])
def send_to_lcd():
pytronics.send_serial(request.form['serial_text'], 9600)
return render_template('/lcd.html')

Assuming the same IP address as before, if you sent an HTTP POST request containing form data serial_text: Hamilton to http://192.168.1.101/send-to-lcd, the send_to_lcd() function would be executed. It would retrieve the word "Hamilton" from the request and send it out the Rascal's serial port at a rate of 9600 bits per second. Then, it would send back a copy of the lcd.html template to your browser.

More detail

If you want more detail, you can start by looking at all the functions that are already in server.py. Try copying and pasting one and changing it to do something new. (When you copy and paste, remember to make the URL and function name unique, or you'll end up in trouble.)

If you want still more detail, you can look at the documentation for Flask, the web framework that the Rascal uses, and Werkzeug, the utility library that Flask uses behind the scenes. The routing section of the Flask quickstart page is a good starting point.