Creating a Route - jordy33/turbogears_tutorial GitHub Wiki

State of Mexico's weather

Install request library:

  • To do that edit setup.py and replace this:
install_requires = [
    "TurboGears2 >= 2.4.3",
    "Beaker >= 1.8.0",
    "Kajiki >= 0.6.3",
    "Mako",
    "zope.sqlalchemy == 1.1",
    "sqlalchemy",
    "alembic",
    "repoze.who",
    "tw2.forms",
    "tgext.admin >= 0.6.1",
    "WebHelpers2"

]

with this:

install_requires = [
    "TurboGears2 >= 2.4.3",
    "Beaker >= 1.8.0",
    "Kajiki >= 0.6.3",
    "Mako",
    "zope.sqlalchemy == 1.1",
    "sqlalchemy",
    "alembic",
    "repoze.who",
    "tw2.forms",
    "tgext.admin >= 0.6.1",
    "WebHelpers2",
    "requests",
    "regex"
]

Go to your project directory and enable the virtual environment using WORKON command with your virtual environment

workon myprojectenv

Then install the dependencies

pip install -e . 

Enable the shell go to project directory and run :

gearbox tgshell -c production.ini

Paste the following code:

import requests
url="http://api.openweathermap.org/data/2.5/weather?id=3523272&appid=d0aa2d3106f2aa9e95523348582e2d6c&units=metric&lang=es"
r=requests.get(url)
print(r.status_code)
print(r.json())
json=r.json()
temperature=json['main']['temp']
pressure=json['main']['pressure']
humidity=json['main']['humidity']
print(temperature)
print(pressure)
print(humidity)

IT would output:

>>> >>> >>> 200
>>> {'main': {'temp_min': 25, 'feels_like': 21.54, 'temp_max': 25, 'pressure': 1028, 'temp': 25, 'humidity': 19}, 'wind': {'speed': 2.06, 'deg': 50}, 'id': 3523272, 'clouds': {'all': 40}, 'sys': {'sunset': 1614818652, 'type': 1, 'country': 'MX', 'sunrise': 1614776150, 'id': 7169}, 'name': 'State of Mexico', 'dt': 1614798221, 'cod': 200, 'coord': {'lon': -99.5, 'lat': 19.3333}, 'base': 'stations', 'weather': [{'icon': '03d', 'main': 'Clouds', 'description': 'nubes dispersas', 'id': 802}], 'timezone': -21600, 'visibility': 9656}
>>> >>> >>> >>> >>> 25
>>> 1028
>>> 

Exit shell with the following command:

exit()

go to the directory:

cd ~/myprojectname/myprojectname/controllers

Edit root.py and insert at the top of the file:

import requests

Then at the end of the file insert the following:

    @expose('myprojectname.templates.weather')
    def weather(self):
        url = "http://api.openweathermap.org/data/2.5/weather?id=3523272&appid=d0aa2d3106f2aa9e95523348582e2d6c&units=metric&lang=es"
        r = requests.get(url)
        json = r.json()
        temperature = json['main']['temp']
        pressure = json['main']['pressure']
        humidity = json['main']['humidity']
        return dict(temperature=temperature,pressure=pressure,humidity=humidity)
    @expose('json')
    def jsonweather(self):
        url = "http://api.openweathermap.org/data/2.5/weather?id=3523272&appid=d0aa2d3106f2aa9e95523348582e2d6c&units=metric&lang=es"
        r = requests.get(url)
        json = r.json()
        temperature = json['main']['temp']
        pressure = json['main']['pressure']
        humidity = json['main']['humidity']
        return dict(temperature=temperature,pressure=pressure,humidity=humidity)

Change Directory:

cd ~/myprojectname/myprojectname/templates

Add the file weather.mak with the following inside:

<HTML>
    <HEAD>
    <TITLE>Weather</TITLE>
    </HEAD>
    <BODY BGCOLOR="FFFFFF">
        <CENTER><IMG SRC="https://goldentroutwilderness.files.wordpress.com/2012/01/various-weather.jpg" height="220" width="220" ALIGN="BOTTOM"> </CENTER>
        <HR>
        <H1>State of Mexico's Weather</H1>
        <p>Temperature: ${temperature} Celcius</p>
        <p>Pressure: ${pressure} Mb </p>
        <p>Humidity: ${humidity} %</p>
    </BODY>
</HTML>

Change Directory:

cd ~/myprojectname

Then execute code:

gearbox serve -c production.ini

Go to safari and open server:
http://test.dudewhereismy.mx:2000/weather
http://test.dudewhereismy.mx:2000/jsonweather

[Back]

⚠️ **GitHub.com Fallback** ⚠️