Skip to content

Instantly share code, notes, and snippets.

@ryderdamen
Last active January 21, 2020 19:26
Show Gist options
  • Save ryderdamen/2571cfd618dc681e8fca6090e5163b65 to your computer and use it in GitHub Desktop.
Save ryderdamen/2571cfd618dc681e8fca6090e5163b65 to your computer and use it in GitHub Desktop.
An example flask application for retrieving and displaying facts about cats.
import os
from flask import Flask, render_template
import requests
app = Flask(__name__)
def get_cat_fact():
cat_facts_url = 'https://catfact.ninja/fact'
response = requests.get(cat_facts_url)
if response.status_code is 200:
return response.json()['fact']
return 'Sorry, could not get cat fact!'
@app.route('/')
def index():
"""Index page of the site"""
cat_fact_string = 'Hello there, here is your cat fact: {}'.format(get_cat_fact())
return render_template('base.html', cat_fact=cat_fact_string)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment