/ friday fun

Friday Fun: Get the news with RSS and GUI Zero

I'll come out and admit it, I am a news junkie, have been since I was a teenager. I like to keep up to date with what is going on, across a variety of topics.

For this week's Friday Fun, I'll show you how to create your own news application for the Raspberry Pi using GUI Zero. It will read the latest news from the BBC and Sky News RSS feeds, RSS = Rich Site Summary, and it is a standard method of breaking web data down into a feed that a computer can then work with. We shall read the feeds, and then print the top three headlines to the application.

alt

GUI Zero?

The Raspberry Pi Foundation has a habit of naming there products / software with a Zero at the end (#blamenuttall)
But GUI Zero is the result of a lot of hard work by Laura better known as @codeboom on Twitter.
Laura wanted to create an easier way for children to code user interfaces for their projects. And crikey she has!

Installing the software

On your Raspberry Pi, which is connected to the Internet we will need to open a terminal to do this. Luckily we only need to install two software packages, and they are feedparser, a Python library for working with RSS feeds, and GUI Zero to create our application.

In the terminal type the following

sudo pip3 install feedparser guizero

This shouldn't take too long to download, and once it has finished you can close the terminal.

Coding In Python

Now let's open the Python 3 editor, found in the Programming menu.

alt

As of Friday 23 June 2017, the Raspberry Pi Foundation have bundled Thonny, a simpler and better featured Python editor with Raspbian. This can also be used to write the code for this project. But I am checking to make sure that it fully works!
This is Thonny installed on my Ubuntu laptop
This is Thonny installed on my Ubuntu laptop

Update 23/6/2017 16:00 BST

Thonny tested and working with this project
alt
I used X forwarding to test Thonny on my Pi 3 using the latest Raspbian image, this meant that I could see the applications, running on my Pi while still sat at my Ubuntu laptop

Update 23/6/2017 13:51 BST

Thonny uses the system installed Python packages, just like we have done to install feedparser using pip3. So it can be used for this project.

---

For the purpose of this blog post we shall proceed with the Python 3 Editor.

When the editor opens, immediately click on File >> New to open a new blank document. Then click on File >> Save and call the file newsround.py. Remember to save your work often!

Our first line of Python code is something new. It will be used later to tell the Python code where to find the Python3 interpreter, enabling our application to work outside of the Python editor.

#!/usr/bin/env python3

Now lets import the libraries that will make this project work. First we import feedparser used to read the news feeds from the websites.

import feedparser

Then we import GUI Zero, but we selectively import the classes that handle creating an application (app), working with text, working with images, and capturing push button input.

from guizero import App, Text, Picture, PushButton

Now lets create two objects, these are dictionaries. Dictionaries use a keyword and value to store information.

Our two dictionaries are used to store the RSS news feeds from BBC and Sky. These feeds are refreshed every time the application is opened.

BBCnews = feedparser.parse("http://feeds.bbci.co.uk/news/rss.xml?edition=uk")
SKYnews = feedparser.parse("http://feeds.skynews.com/feeds/rss/uk.xml")

We now move on to starting the code that will make up the graphical element of the application.
Our "app" is a container. It will store the elements that make up our application (text, graphics, buttons). Our needs a title, and we can specify how big the interface window will be.

app = App(title="News Roundup", width=700, height=450)

An application full of text is boooorrrinng! So lets add some images. GUI Zero can only work with GIF images, and sadly not animated gifs

via GIPHY

We create an object called BBC_Logo and in there we use the Picture class to instruct GUI Zero to place the BBC News logo in the GUI.

BBC_Logo = Picture(app, image="bbcnews.gif")

Lets get some news in the application. We use a for loop, with a range of 3..in other words it will iterate 3 times. The Text that we wish to see in the app is taken from the BBCnews dictionary and we are looking in the entries for the first 3 news items, and then we get their title (headlines). This is then rendered to the application using a size 16 Arial font in black.

for i in range(3):
    Text(app, text = BBCnews["entries"][i]["title"], size=16, font="Arial", color="black")

We repeat the logo and for loop process for Sky News.

SKY_Logo = Picture(app, image="sky-news-logo.gif")
for i in range(3):
    Text(app, text = SKYnews["entries"][i]["title"], size=16, font="Arial", color="black")

Our app is almost complete, but... we need a way to close the application. Sure we can use the X in the top right of the window, but come on we can do better than that!
Our Close object is a PushButton that will send a command to the application. This command is destroy a rather brutal way of saying "Please close the application, I have read the headlines." We also give the button some text to describe its function.

Close = PushButton(app, command=app.destroy, text="Close News")

Our final line of Python instructs the application to display all of the work that we have done in the application.

app.display()

Complete Code Listing

Your code should look like this.

#!/usr/bin/env python3
import feedparser
from guizero import App, Text, Picture, PushButton


BBCnews = feedparser.parse("http://feeds.bbci.co.uk/news/rss.xml?edition=uk")
SKYnews = feedparser.parse("http://feeds.skynews.com/feeds/rss/uk.xml")
app = App(title="News Roundup", width=700, height=450)
BBC_Logo = Picture(app, image="bbcnews.gif")
for i in range(3):
    Text(app, text = BBCnews["entries"][i]["title"], size=16, font="Arial", color="black")
SKY_Logo = Picture(app, image="sky-news-logo.gif")
for i in range(3):
    Text(app, text = SKYnews["entries"][i]["title"], size=16, font="Arial", color="black")
Close = PushButton(app, command=app.destroy, text="Close News")
app.display()

Give it a go!

alt
Run your code, in the Python 3 Editor (IDLE) click on Run >> Run Module and you will see your application appear on screen.

If you are using Thonny, click on the Green "play button" arrow to start the code.

Well done you have made your own application using GUI Zero!

Bonus Content

Running the application from inside Python 3 Editor or Thonny is cool! But what is even cooler? Running the application on it's own!!!
We have already done some of the work for this, remember the first line of code?

#!/usr/bin/env python3

Well that tells our application where to look for the Python 3 interpreter. So now all we need to do is make our application code executable. To do this we need to use a Terminal, navigate to where you saved the neswround.py file and enter this command to modify the file to become executable.

chmod +x newsround.py

Now you can run the command by typing.

./newsround.py

You can also make your own desktop shortcut that will call the application by double clicking on an icon, and that is a project that you can research and try for yourself :D

What did we accomplish?

We made an application, in 14 lines of Python code that

  • Parsed RSS news feeds
  • Saved the data to dictionaries
  • We used a key to retrieve values from the dictionaries
  • We used a for loop to automatically retrieve the first 3 entries in the dictionaries
  • We created a button to close our application

Great work everyone!


Note to the BBC and Sky

Hi there, thanks for reading this far!
The use of your logos is for a purely educational project, no profit or mis-representation has been, or will be derived from this project. It is merely an educational and fun project for those learning to code to reference.