Python Friday #238: Create a Dashboard for FastAPI

We already created a shared layout for the web interface of our API and fixed the problem with the static resources. Now we can create a dashboard that shows us some important numbers about our application.

This post is part of my journey to learn Python. You find the code for this post in my PythonFriday repository on GitHub.

 

Create a Pydantic model for the statistics

We can start our work with a StatisticOverview class and put it into the new file models/statistics.py:

This model helps us to transfer our numbers from our data store to the web interface.

 

Create a test for the new behaviour

To fill our StatisticOverview model with data, we will need a new method on our data store. Before we add that, we create a test that should check if the data we get back makes any sense:

 

Implement the statistic gathering

With the test in place, we can now implement the get_statistics() method inside the data/datastore_db.py file:

Unfortunately, there is no simple way to count rows in SQLAlchemy and I had to use a lot of magic to get the numbers we are interested in. The code above runs three times a SELECT COUNT(*), a first time to count all the tasks, a second time for all tasks that are done and a last time for all tasks that are still open. We get the data back from the database and put it into our Pydantic model.

 

Extract the dependency get_db()

We connect to the database through our get_db() method that is inside our routers/todo.py file. Since we want to put our dashboard endpoint somewhere else, we first need to move the get_db() method in a location we can access from both places. We can create a file dependencies.py next to main.py and move the method to its new location:

We not only need to fix the import in our router, but also in our tests. We can now run the tests and the dependency replacement should still work.

 

Add a test for the dashboard

Before we implement the dashboard, we create a test to check that our dashboard contains the right data and that the total number matches the sum of open and done tasks:

 

Add the dashboard endpoint

In our main.py file we reference our newly moved get_db() method and write the code for our dashboard endpoint:

The jsonable_encoder() method turns our Pydantic model into a JSON structure that allows us to use the field names inside {{ }} blocks to show the values in our new template/dashboard.html file:

With the template in place, we can run our tests, and everything should pass.

The dashboard shows our task numbers.

 

Next

With our little dashboard in place, we can share some basic data about our application in a user-friendly way. We can extend on the templates and the static route and create a full API-driven web application.

While writing the query for the statistics method, I noticed that our SQLAlchemy implementation still runs synchronously. Next week I show you all the necessary steps to use an asynchronous SQLAlchemy.

2 thoughts on “Python Friday #238: Create a Dashboard for FastAPI”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.