Skip to content

Instantly share code, notes, and snippets.

@rsperl
Last active July 25, 2022 16:14
Show Gist options
  • Save rsperl/dba09cba1e1e85076fa29cf194890c1c to your computer and use it in GitHub Desktop.
Save rsperl/dba09cba1e1e85076fa29cf194890c1c to your computer and use it in GitHub Desktop.
flask app template #python #rest #snippet
#!/usr/bin/env python3
"""
Documentation
See also https://www.python-boilerplate.com/flask
"""
import os
import argparse
from flask import Flask, jsonify, send_file
from flask_cors import CORS
def create_app(config=None):
app = Flask(__name__)
# See http://flask.pocoo.org/docs/0.12/config/
app.config.update(dict(DEBUG=True, SECRET_KEY="development key"))
app.config.update(config or {})
# Setup cors headers to allow all domains
# https://flask-cors.readthedocs.io/en/latest/
CORS(app)
# Definition of the routes. It's probably a good idea to break them out
# into their own file soon. See also Flask Blueprints:
# http://flask.pocoo.org/docs/0.12/blueprints
@app.route("/")
def hello_world():
return "Hello World"
@app.route("/foo/<someId>")
def foo_url_arg(someId):
return "Test: " + someId
return app
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--port", action="store", default="8000")
args = parser.parse_args()
port = int(args.port)
app = create_app()
app.run(host="0.0.0.0", port=port)
"""
This file demonstrates common uses for the Python unittest module with Flask
Documentation:
* https://docs.python.org/2/library/unittest.html
* http://flask.pocoo.org/docs/latest/testing/
"""
import random
import unittest
import main
class FlaskTestCase(unittest.TestCase):
""" This is one of potentially many TestCases """
def setUp(self):
app = main.create_app()
app.debug = True
self.app = app.test_client()
def test_route_hello_world(self):
res = self.app.get("/")
# print(dir(res), res.status_code)
assert res.status_code == 200
assert b"Hello World" in res.data
def test_route_foo(self):
res = self.app.get("/foo/12345")
assert res.status_code == 200
assert b"12345" in res.data
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment