Last update: 2019-09-27

Making a weather status screen in Python


As part of my Raspberry Pi powered status script I show the current weather for my city of Montreal. At first I used the widget provided by Weather Canada, but I decided to move to a custom web page by using a simple Python script that reads the RSS feed from the same Weather Canada service, and then show the result using Font Awesome icons and the Bootstrap framework.

This is what the result looks like:

The first thing to do is import the needed libraries, including feedparser which allows us to parse the RSS feed, then connect to the feed and extract the information. Finally, we display a bootstrap table with the data for the few next upcoming days.

This is the complete code:

<python>
#!/usr/bin/env python3

import os
import sys
import time
import connix
import feedparser
import re

# This is the feed URL
rss = feedparser.parse("https://weather.gc.ca/rss/city/qc-147_e.xml")

print(connix.header())

print("<html><head><link href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' rel='stylesheet' /><link href='https://use.fontawesome.com/releases/v5.8.1/css/all.css' rel='stylesheet' /><style>body{background-color:#000000;color:#FFFFFF;font-family:Verdana;}*{font-size:24px!important;}</style></head><body>")

def get_temp(t):
    # Return the current temperature information from the title
    return str(t).split(':')[1]

def get_icon(a):
    # Return an appropriate icon based on the title information
    icon = "<i class='fa fa-question'></i>"
    if "rain" in str(a).lower():
        icon = "<i class='fa fa-cloud-rain'></i>"
    elif "shower" in str(a).lower():
        icon = "<i class='fa fa-cloud-showers-heavy'></i>"
    elif "cloud" in str(a).lower():
        icon = "<i class='fa fa-cloud'></i>"
    elif "snow" in str(a).lower():
        icon = "<i class='fa fa-snow'></i>"
    elif "sun" in str(a).lower():
        icon = "<i class='fa fa-sun'></i>"
    elif "clear" in str(a).lower():
        icon = "<i class='fa fa-cloud-sun'></i>"
    return icon

print("<table class='table table-bordered'>")
print("<tr><td><b>Currently:</b></td><td>{} {}</td></tr>".format(get_icon(rss['items'][1]['title']), get_temp(rss['items'][1]['title'])))
print("<tr><td><b>{}:</b></td><td>{} {}</td></tr>".format(str(rss['items'][2]['title']).split(':')[0], get_icon(rss['items'][2]['title']), get_temp(rss['items'][2]['title'])))
print("<tr><td><b>{}:</b></td><td>{} {}</td></tr>".format(str(rss['items'][3]['title']).split(':')[0], get_icon(rss['items'][3]['title']), get_temp(rss['items'][3]['title'])))
print("<tr><td><b>{}:</b></td><td>{} {}</td></tr>".format(str(rss['items'][4]['title']).split(':')[0], get_icon(rss['items'][4]['title']), get_temp(rss['items'][4]['title'])))
print("<tr><td><b>{}:</b></td><td>{} {}</td></tr>".format(str(rss['items'][5]['title']).split(':')[0], get_icon(rss['items'][5]['title']), get_temp(rss['items'][5]['title'])))
print("<tr><td><b>{}:</b></td><td>{} {}</td></tr>".format(str(rss['items'][6]['title']).split(':')[0], get_icon(rss['items'][6]['title']), get_temp(rss['items'][6]['title'])))
print("</table>")

Looking to expand into the cloud? See what a certified freelancer can do for you.


© 2007-2019 Patrick Lambert - All resources on this site are provided under the MIT License - You can contact me at: contact@dendory.net