Building Python applications with Postgres and FastAPI
Last updated
Last updated
In this guide, we will walk through the process of creating a Python application using together with Postgres using Neurelo.
Our goal is to create a "Fun Facts" application. This app will display a random animal fact to the user upon each site refresh. The facts, stored in a database on a remote server, will be managed through Python, with FastAPI serving as our chosen web framework.
Central to our application is Neurelo which will significantly simplify the database interactions by leveraging Neurelo’s auto-generated API endpoints.
Before we dive into building this application, let us lay the groundwork. This involves creating a new project, setting up a data source, and formulating a database schema suitable for our app. For this, we will use Postgres.
The first step is to . Following the project creation, .
Next, a database schema must be defined. For the sake of simplicity, our schema will include the following fields: id
, animal_name
, fact
, and created_at
. With this, we can as:
Once migrations are completed, your database will be updated to match your schema in Neurelo. At this point, you are ready to use Neurelo's auto-generated API endpoints for your project. You can view these in the environment’s APIs tab.
It's now time to develop the application code in Python. We will begin by creating a class named Neurelo
to facilitate interaction with Neurelo’s API endpoints, and consequently, our database. The code for this is straightforward:
And that's it! We have now established an interface with our database via Neurelo. This setup helps us to integrate this class with our preferred web framework to build our application. As previously mentioned, we will be using FastAPI to illustrate this process,
The server mentioned above includes an endpoint (/random-fact/
) that retrieves a random fact from the animal_facts
table via the Neurelo
class, returning an HTML response rendered with a Jinja2 template. This code is assumed to be in a main.py
file. The template, fact_page.html
, can be straightforward:
This template uses Jinja2 syntax to display the random fact. That's all there is to it! You can launch your FastAPI application with uvicorn main:app --reload
and go to http://127.0.0.1:8000/random-fact/
to see your application powered by Neurelo in action.
To begin, go to the Definitions view and click on the Custom Queries tab.
Next, create a new custom query endpoint as shown below:
For more information on Neurelo’s Schema Language, see the .
Once your schema is ready, and then .
After the environment is set up, applying a migration to your data source is straightforward. Just follow .
We provide an to easily run and test these auto-generated APIs!
Before running this code, it is essential to . Subsequently, store this key in an .env
file as X_API_KEY="<KEY>"
. Ensure the .env
file is located in the same directory as your Python script.
As can be imagined, this code can be easily adapted to other web frameworks, such as .
Now, imagine your database holds thousands of random animal facts, and you wish to avoid fetching all to select one on the server side. While you can use filters and other capabilities in Neurelo's auto-generated APIs for such queries, we will use feature to create custom endpoints as an example here.
You can use Neurelo's for swift construction of a custom query. For this scenario, the prompt we will use is "fetch a random animal fact".
And there you have it! The endpoint and its corresponding query are ready in just a few seconds. Our is also available for convenient query testing and iterations, if needed. The remaining step is to commit this new custom query definition, update our environment to incorporate this change, and then proceed with using the new endpoint.
You can read more on in our documentation. Do not forget to update the self.api_url
in the Neurelo
class with your new custom endpoint, which, in this example, is /custom/random_animal
.