Flask Python API to fetch data from Cemantix/Cemantle Notion database.
Find the code on the repo —> here
ℹ️ Disclaimer: I did this project to learn Flask basics, so feel free to give me your feedbacks
Flask API for Cemantix Statistics
We will explore a Flask API implementation that retrieves and provides statistics for the Cemantix application. The API interacts with a Notion database to fetch relevant data and exposes it in a structured format.
Introduction
The Flask API is designed to serve as an interface for retrieving statistics related to the Cemantix application. The Cemantix application relies on the Notion platform to store and manage data. By leveraging the Notion API, the Flask API interacts with the Cemantix Notion database to retrieve information such as the word of the day, elapsed time, number of requests, and the date associated with the statistics.
Dependencies
Before running the Flask API, make sure you have the following dependencies installed:
- Python 3.x: The programming language used for the Flask API.
- pip: The package installer for Python.
- Virtual environment (optional): Recommended for managing project dependencies.
To install the required dependencies, follow these steps:
-
Set up a virtual environment (optional): It’s recommended to create a virtual environment to isolate the project’s dependencies. You can create a virtual environment using
venv
orconda
. Here’s an example usingvenv
:python3 -m venv myenv source myenv/bin/activate
-
Install the dependencies: In the project directory, run the following command to install the required packages:
pip install -r requirements.txt
This command will install Flask, Flask-Cors, requests, and python-dotenv along with their specified versions.
API Endpoints
The Flask API provides a single endpoint /
that returns the Cemantix statistics in JSON format. Let’s explore the code:
@app.route('/')
def get_stats():
return jsonify(build_stats()), 200
The get_stats
function is decorated with @app.route('/')
, which means it handles requests to the root URL of the API. When a request is made to this endpoint, the function build_stats
is called to fetch the Cemantix statistics, which are then returned as a JSON response.
The build_stats
function is responsible for retrieving data from the Cemantix Notion database. It utilizes the provided Notion tokens and database IDs to make authenticated requests to the Notion API. The retrieved data is then processed and organized into a dictionary containing statistics for both the “cemantix” and “cemantle” sections.
Retrieving Cemantix Data
To fetch data from the Cemantix Notion database, the Flask API uses the fetch_cemantix_data
function. This function takes a token and database ID as parameters and makes a POST request to the Notion API with the specified payload. The response is returned for further processing.
The Flask API provides several helper functions to extract specific information from the fetched data:
retrieve_word_of_the_day
: Retrieves the word of the day from the fetched data.retrieve_elapsed_time
: Retrieves the elapsed time from the fetched data.retrieve_requests_number
: Retrieves the number of requests from the fetched data.retrieve_word_date
: Retrieves the date associated with the statistics from the fetched data.
These functions access the relevant properties in the JSON response and extract the required information.
Cross-Origin Resource Sharing (CORS)
To allow requests from specific origins, the Flask API utilizes the Flask-Cors extension. The CORS
function is called with the origins
parameter, which specifies the allowed origins. In this case, requests from http://localhost:5173
and https://cemantix-ui.vercel.app
are allowed.
Running the API
To run the Flask API, ensure that the required dependencies are installed. Create a .env
file in the same directory as the Flask API script and provide the necessary environment variables:
CEMANTIX_NOTION_TOKEN=<your_cemantix_notion_token>
CEMANTIX_DATABASE_ID=<your_cemantix_database_id>
CEMANTLE_NOTION_TOKEN=<your_cemantle_notion_token>
CEMANTLE_DATABASE_ID=<your_cemantle_database_id>
Replace <your_cemantix_notion_token>
, <your_cemantix_database_id>
, <your_cemantle_notion_token>
, and <your_cemantle_database_id>
with the appropriate values.
Once the environment variables are set, run the Flask API script. The API will be accessible at http://localhost:5000/
. Send a GET request to this URL to retrieve the Cemantix statistics in JSON format.
Conclusion
The Flask API presented here provides a convenient way to retrieve statistics for the Cemantix application. By leveraging the Notion API and Flask’s simplicity, the API interacts with the Notion database and exposes the statistics in a structured format. This API can be utilized by various clients to access and utilize the Cemantix statistics for further analysis or visualization.