Python rest API client

In the example we will connect to weather API to get temperature by city name using Requests.  We will use Flask and Jinja to render HTML form and the result.

Register and get API key at

Create files:

from flask import Flask, render_template, request
import requests

app = Flask(__name__)

@app.route('/temperature', methods=['POST'])
def temperature():
    city = request.form['city']

    r = requests.get(''+city+'&units=metric')
    json_object = r.json()
    temp = json_object['main']['temp']
    return render_template('temperature.html', temp=temp)

def index():
    return render_template('index.html')

if __name__ == '__main__':


<!doctype html>
        <title>Request example</title>
    <form action="/temperature" method="post">
        City: <input type="text" name="city">
        <input type="submit">


<!doctype html>
    <title>Current Temperature</title>
        <h1>The current temperature (C) is: {{temp}} degrees.</h1>

Run. And navigate to localhost:5000





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s