Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flask demo: don't provide a meaningless default server #163

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,13 @@ print(patient.name[0].given)
Take a look at
[flask_app.py](https://github.com/smart-on-fhir/client-py/blob/main/demos/flask/flask_app.py)
to see how you can use the client in a simple (Flask) app.
This app starts a web server,
listening on [_localhost:8000_](http://localhost:8000),
and prompts you to log in to our sandbox server and select a patient.

This demo requires a server that is capable of SMART OAuth logins for patients,
so make sure you have such a server ready first.

This app will start a web server,
listen on [_localhost:8000_](http://localhost:8000),
and prompt you to log in to our sandbox server and select a patient.
It then retrieves the selected patient's demographics and med prescriptions
and lists them on a simple HTML page.

Expand All @@ -192,6 +196,7 @@ and install the needed packages as shown:
python3 -m venv env
. env/bin/activate
pip install -r requirements.txt
# Edit flask_app.py and put your own server's URL as api_base.
./flask_app.py


Expand Down
22 changes: 12 additions & 10 deletions demos/flask/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@
# app setup
smart_defaults = {
'app_id': 'my_web_app',
# The CapabilityStatement being returned from the new test server,
# r4.smarthealthit.org, is currently missing a required field in the
# description of one of the search params. So you may see a
# 'Non-optional property "type"' error message when running this with
# the default config. We'll remove this message once that has been
# fixed and tested.
'api_base': 'https://r4.smarthealthit.org/',
'api_base': None,
'redirect_uri': 'http://localhost:8000/fhir-app/',
}

Expand All @@ -29,8 +23,10 @@ def _get_smart():
state = session.get('state')
if state:
return client.FHIRClient(state=state, save_func=_save_state)
else:
elif smart_defaults['api_base']:
return client.FHIRClient(settings=smart_defaults, save_func=_save_state)
else:
return None

def _logout():
if 'state' in session:
Expand Down Expand Up @@ -80,8 +76,13 @@ def index():
"""
smart = _get_smart()
body = "<h1>Hello</h1>"

if smart.ready and smart.patient is not None: # "ready" may be true but the access token may have expired, making smart.patient = None

if smart is None:
body += """<p>Please edit flask_app.py and set a value for 'api_base', """
body += """pointing at your own OAuth-capable server.</p>"""

# "ready" may be true but the access token may have expired, making smart.patient = None
elif smart.ready and smart.patient is not None:
name = smart.human_name(smart.patient.name[0] if smart.patient.name and len(smart.patient.name) > 0 else 'Unknown')

# generate simple body text
Expand All @@ -92,6 +93,7 @@ def index():
else:
body += "<p>(There are no prescriptions for {0})</p>".format("him" if 'male' == smart.patient.gender else "her")
body += """<p><a href="/logout">Change patient</a></p>"""

else:
auth_url = smart.authorize_url
if auth_url is not None:
Expand Down