Getting started with Flask in nbdev.

Getting started

The goal of this notebook is to show how you can create and run smart_on_fhir_client_py_demo/flask_hello_world.py on your own machine.

Please see index.ipynb and set things up to run on your own machine.

Code and test a simple flask app

create_app[source]

create_app(test_config=None)

Create and configure an instance of the Flask application.

↓ make sure we can create the app with/without specifying config

assert not create_app().testing
assert create_app({"TESTING": True}).testing

↓ make requests against a test client to show that we can GET the /hello route with/without specifying a name

app = create_app({"TESTING": True})
client = app.test_client()
response = client.get("/hello/Pieter van de Heuvel, MSc")
assert response.data == b"Hello, Pieter van de Heuvel, MSc!"
response = client.get("/")
assert response.data == b"Hello, World!"

The next cell just says "run the app if we're running from the command line". We use IN_NOTEBOOK to avoid running the app when we're in the notebook.

Convert this notebook to a python module

After making changes to this notebook, we need to get nbdev to re-create the flask_hello_world.py file.

We can do this in code ↓ or from the command line with nbdev_build_lib

from nbdev.export import notebook2script
notebook2script('50_flask_app.ipynb')

Run the flask app

Now we can run python smart_on_fhir_client_py_demo/flask_hello_world.py from the command line and hit URLs like