Using Python’s Flask to Build a Basic API

Prerequisites

Mission: Build a Flask based API.

Flask Installation

$ python --version
Python 3.7.3
$ which python
/Users/adron/Codez/python-env-examples/bin/python
pip install Flask

First Code

from flask import Flask
app = Flask(__name__)
@app.route('/')
def heyo_mofo():
return 'Heyo, Sup!'
export FLASK_APP=server.py
flask run
export FLASK_ENV=development
export FLASK_APP=server.py
flask run

Routing With Routes

from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Index Page'
@app.route('/datum')
def hello():
return 'The data to provide!'
@app.route('/unit/<uuid:unit_id>')
def show_unit(unit_id):
return 'Unit ID %d, albeit this would usually be used to get the post details and body from a database to present.' % unit_id
@app.route('/path/<path:subpath>')
def show_subpath(subpath):
return 'Showing the subpath after the /path/ - Subpath %s' % escape(subpath)
@app.route('/efforts/')
def projects():
return 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam scelerisque tellus sed magna pulvinar egestas. Donec vitae diam in eros porta mollis. Vestibulum sagittis lorem id dolor luctus, quis tincidunt nulla dictum. Etiam ac vulputate massa. Nullam aliquet arcu imperdiet, mattis mi sed, rutrum lectus. Phasellus viverra leo et mi dapibus tincidunt. Nulla facilisi. Cras eget metus turpis. Etiam a elit arcu. Pellentesque ac eros ligula.'
@app.route('/about')
def about():
return 'Morbi rhoncus congue justo id malesuada. Mauris semper mattis dui. Etiam sodales dui vitae tincidunt iaculis. Nam id velit accumsan, aliquam lorem ac, ultrices nisl. Aenean non lectus tellus. Mauris rutrum metus ut condimentum efficitur. Nulla a dolor felis. Aenean congue turpis vitae felis commodo, vitae blandit dolor varius. Duis faucibus neque dolor, eu sollicitudin lacus lacinia vel. Etiam hendrerit, nibh vitae porta vestibulum, odio metus sollicitudin justo, in lobortis metus nisi fermentum justo. Cras pellentesque vel nunc posuere fermentum.'
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Index Page'
@app.route('/datum')
def hello():
return 'The data to provide!'
@app.route('/unit/<uuid:unit_id>')
def show_unit(unit_id):
return 'Unit ID %d, albeit this would usually be used to get the post details and body from a database to present.' % unit_id
@app.route('/path/<path:subpath>')
def show_subpath(subpath):
return 'Showing the subpath after the /path/ - Subpath %s' % escape(subpath)
NameError: name ‘escape’ is not defined
from glob import escape

Converters

@app.route('/efforts/')
def projects():
return 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam scelerisque tellus sed magna pulvinar egestas. Donec vitae diam in eros porta mollis. Vestibulum sagittis lorem id dolor luctus, quis tincidunt nulla dictum. Etiam ac vulputate massa. Nullam aliquet arcu imperdiet, mattis mi sed, rutrum lectus. Phasellus viverra leo et mi dapibus tincidunt. Nulla facilisi. Cras eget metus turpis. Etiam a elit arcu. Pellentesque ac eros ligula.'
@app.route('/about')
def about():
return 'Morbi rhoncus congue justo id malesuada. Mauris semper mattis dui. Etiam sodales dui vitae tincidunt iaculis. Nam id velit accumsan, aliquam lorem ac, ultrices nisl. Aenean non lectus tellus. Mauris rutrum metus ut condimentum efficitur. Nulla a dolor felis. Aenean congue turpis vitae felis commodo, vitae blandit dolor varius. Duis faucibus neque dolor, eu sollicitudin lacus lacinia vel. Etiam hendrerit, nibh vitae porta vestibulum, odio metus sollicitudin justo, in lobortis metus nisi fermentum justo. Cras pellentesque vel nunc posuere fermentum.'

--

--

--

Software dev, data, heavy metal, transit, economics, freethought, atheism, cycling, livability, beautiful things & adrenaline junkie.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Engineering & Data Science @ TravelTriangle — Building Complex and Scalable Holiday Marketplace…

Reading Up on Observability and Monitoring

Onwards and Forwards and Such

Git ignore already modified files

LearnFactory Internship: Day 48 (July 6, 2018)

Making a RTS game #2: Adding a very basic UI (Unity/C#)

Server-side I/O Performance: Node vs. PHP vs. Java vs. Go

Basics of Amazon EC2 That You Must Know

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adron Hall

Adron Hall

Software dev, data, heavy metal, transit, economics, freethought, atheism, cycling, livability, beautiful things & adrenaline junkie.

More from Medium

How to make an application using serverless application model (SAM) and python

Build Portfolio by Publishing Your Project to pypi.org

How to Upgrade All Python Packages with PIP in 2022?

CreateURL — not your typical URL shortener.