Python is glue. We can use it to join different elements of code. As a language, Python is easy to learn and human-readable, making it one of the most effective languages for learning and general-purpose programming. Part of the charm of Python is the many modules of code that can be easily inserted into a project.
Thonny is a powerful yet simple editor for Python and with the release of version 4 we wanted to use it to create a project. In this tutorial, we will use the latest version of Thonny to create a web application that will pull data from Raspberry Pi actions from rpilocator.com and use it to fill in a table in our app.
RSS is a great way to share a stream of information. It can be used to publish news headlines, such as the Tom’s Hardware RSS Feed either even the latest xkcd comic strip.
Please note that the RSS feed for rpilocator is not as up-to-date as the data for rpilocator.com. Think of this project more as a notification system than a “sniping” tool.
Installation of Thonny 4.0
Thonny is the default Python IDE on the raspberry pi, but it is not limited to just that machine. Thonny is also available for Windows, Mac, and Linux machines, and can be used to write Python and MicroPython for devices like the Raspberry Pi Pico W and ESP32.
1. open a browser to Thonny’s home page.
two. Select the download for your operating system. For Windows, there are a few options to choose from. The first option is which version of Python we would recommend the latest (3.10 at the time of writing). Next is your choice to install Thonny on your machine or use a portable version. We recommend installing Thonny on your machine.
3. Click on the downloaded file to start the installation.
Four. Click “More Info” to continue with the installation. The new installation has a certificate that is relatively unknown and has yet to build a reputation.
5. Click “Run Anyway” to continue.
6. Press next to continue.
7. Accept the license agreement.
8. Select the check box to create a desktop icon. This is an optional step. We chose not to because we prefer the icons on the taskbar.
9. Click Install to start the installation process.
10 Click Finish to finish the installation.
Creating our project with Thonny 4.0
Thonny is focused on beginners, but don’t be fooled, Thonny is a competent and full-featured editor for creators. Thonny has a multi-window layout that can be edited to suit his needs.
1. Records: This is a basic file manager that can be used to open files in a project. Raspberry Pi Pico W and other MicroPython devices will open an additional panel that we can use to copy files to and from the device.
two. Coding area: This is where we create the project for our code. We can have multiple tabs, for multiple files.
3. Python shell: Python Shell (REPL, Read, Eval, Print, Loop) is where we can see the output of our code and also interact with it.
Four. Attendee: If your code has a bug or doesn’t follow a style guide, it will be flagged here.
Installing modules with Thonny
Python modules (sometimes also called “libraries”) are prewritten code segments that allow additional functionality. Popular examples include RPI.GPIO and GPIO Zero for the Raspberry Pi. Modules often abstract/simplify complex tasks. In our project we will use two modules. PyWebIO is a module for creating HTML content using Python. It also creates a web server that we can use to quickly connect to our application. The second module is Feedparser, an RSS feed reader module that we will use to read the stock level feed from the Raspberry Pi rpilocator.
1. Open Thonny and make sure there are no open projects.
two. Click Tools >> Manage Packages. Thonny has a built in GUI for the Python 3 “pip” package manager.
3. Search pywebio.This is the module that we will use to generate a web page using Python.
Four. Click Install to download and install the module.
5. Repeat the steps above, this time install feedparser. Feedparser is a Python module for RSS feeds.
6. Click Close to exit the dialog.
Write project code
Our goal is to create a Python project that uses the data from the rpilocator RSS feed to populate a table. We’ll take the current five inputs and display them in an HTML table, created with Python.
1. In a new blank document, import two pywebio modules. The first contains the code to start a simple web server. The pywebio.output module is used to output HTML elements like tables and hyperlinks.
from pywebio import start_server from pywebio.output import *
two. Import the feedparser module.
3. Create a function called main.
Four. Inside the function, create an object, “stock”, and use it to store the parsed output of the rpilocator RSS feed.
stock = feedparser.parse('https://rpilocator.com/feed/')
5. Create three empty lists, in_stock, in_stock_link, and category. These will be used to store the data retrieved from the “stock” object that contains the RSS data.
in_stock =  in_stock_link =  category = 
6. Create a for loop that will iterate five times.
for i in range(5):
7. Use “add” to add the stock status, link and category (reseller name) to the corresponding list. RSS data stored in “stock” is a mixture of lists and dictionaries. For data in a list we can use its numeric index, which is the value of i in our for loop. This will count from 0 to 4 as the for loop iterates. Data stored in a dictionary requires that we know the key (‘entries’ for example). Using the key will return its value.
in_stock.append(stock['entries'][i]['title']) in_stock_link.append(stock['entries'][i]['link']) category.append(stock['entries'][i]['category'])
8. Outside of the for loop, create a popup notification using “toast”. The message can be a mix of loud and even emojis.
toast('🍓I found Raspberry Pi in stock!🍓')
9. Use “put_html” to write an HTML H1 header element to the web page. We can use this function to write any HTML element on the page, but note that the PyWebIO module has many different means of creating specialized items.
put_html("<h1>Raspberry Pi Stock</h1>")
10 Create a list, “table” and use it to store two columns of data, taken from our in_stock, in_stock_link, and category lists. The first row is the Details and URL column headings. In Stock will print a brief description of what is in stock. Using “put_link” we create an HTML hyperlink, with the link text being the reseller’s name, stored in the category list, and the address stored in in_stock_link.
table = [['Details','URL'], [in_stock, put_link(category,url=in_stock_link)], [in_stock, put_link(category,url=in_stock_link)], [in_stock, put_link(category,url=in_stock_link)], [in_stock, put_link(category,url=in_stock_link)], [in_stock, put_link(category,url=in_stock_link)], ]
eleven Use PyWebIO’s “put_table” function to create an HTML table from our table object.
12 Use “put_link” to create a hyperlink below the table, in this case it takes us to the source of the Raspberry Pi stock levels, rpilocator.
put_link('Data provided by RPiLocator',url="https://rpilocator.com")
13 Outside the function, call the PyWebIO “start_server” function and pass it three arguments. The arguments are our “main” function that will create the table from the RSS data. The port is set to 8080 and debugging is enabled through the Python shell and on our web page.
start_server(main, port=8080, debug=True)
14 Save the code as RSS-Feed-Reader.py and click Run to start.
fifteen. Click the link in the Python shell to open the web page in your default browser.
Complete list of codes
from pywebio import start_server from pywebio.output import * import feedparser def main(): stock = feedparser.parse('https://rpilocator.com/feed/') in_stock =  in_stock_link =  category =  for i in range(5): in_stock.append(stock['entries'][i]['title']) in_stock_link.append(stock['entries'][i]['link']) category.append(stock['entries'][i]['category']) toast('🍓I found Raspberry Pi in stock!🍓') put_html("<h1>Raspberry Pi Stock</h1>") table = [['Details','URL'], [in_stock, put_link(category,url=in_stock_link)], [in_stock, put_link(category,url=in_stock_link)], [in_stock, put_link(category,url=in_stock_link)], [in_stock, put_link(category,url=in_stock_link)], [in_stock, put_link(category,url=in_stock_link)], ] put_table(table) put_link('Data provided by RPiLocator',url="https://rpilocator.com") start_server(main, port=8080, debug=True)