omni_python allows you to seamlessly integrate Python code within Omnigres, which are then used as stored procedures in the database.
omni_python also allows you to integrate Flask framework and serve HTTP requests from directly within Omnigres.
omni_python extension if it is not installed.
We would also need some helper extensions for loading relevant files from filesystem.
Create a directory on your host system containing the Python files, and corresponding
requirements.txt, and mount the volume to the Docker container.
We need to add the following to
requirements.txt file to support Python integration:
Create Python files. You can create more than one files, as long as they are in the directory that you would mount as a volume inside the Docker container for running Omnigres.
Create functions in Python as you would, and annotate them with
@pg to make sure they are loaded into the database.
For example, let's make two Python files.
We are working on a CLI tooling that will take care of directory mapping.
Load the filesystem files.
You can set a reload command for reloading the filesystem changes.
Run Omnigres in a Docker container. Make sure to mount the local directory as a volume on the correct path.
Let's try it out!
For Flask framework integration, we have a few more steps.
Ensure to create
omni_httpd extension to be able to handle HTTP requests.
Add the following in
Let's say you have a table called
Now you can update your Python files (in the mounted volume) to include Flask functionality. For example, you can define endpoints to fetch list of all employees, fetch a particular employee, as well as create a new employee record.
from omni_python import pg from omni_http import omni_httpd from omni_http.omni_httpd import flask from flask import Flask, jsonify, make_response, request import uuid app = Flask('myapp') def employees_to_json(employees): return json.dumps([dict(employee) for employee in employees]) @app.route('/employees', methods=['POST']) def create_employee(): json_data = json.loads(request.data.decode('UTF-8')) employee_name = json_data.get('name') employee_department = json_data.get('department') employee_salary = json_data.get('salary') if not employee_name or not employee_department or not employee_salary: return "Missing required fields", 400 employee = plpy.execute(plpy.prepare("insert into employees (name, department, salary) " "values ($1, $2, $3) returning *", ["text", "text", "int"]), [employee_name, employee_department, employee_salary]) return employees_to_json(employee) @app.route('/employees', methods=['GET']) def get_employees(): employees = plpy.execute(plpy.prepare("select * from employees")) return employees_to_json(employees) @app.route('/employees/<int:employee_id>', methods=['GET']) def get_employee(employee_id): employee = plpy.execute(plpy.prepare("select * from employees where id = $1", ["int"]), [employee_id]) return employees_to_json(employee) handle = pg(flask.Adapter(app))
Flask integration with Omnigres
flask.Adapter(app) creates an instance of the flask.Adapter class, which is provided by the
This adapter allows you to integrate Flask with the
app object is your Flask application instance, and you pass it to
flask.Adapter() to create an adapter
that can handle HTTP requests using your Flask app.
handle function is the entry point for handling incoming HTTP requests.
It takes an
HTTPRequest object as input and is expected to return an
Inside the function, it forwards the
req object to the
app_ object, which is a Flask
application wrapped in the
`flask.Adapter(). This allows the Flask application to handle
the incoming HTTP request and generate a response. Finally, the response is returned
Note: We use
plpy for now, but we should use DB API compatible APIs and/or other
frameworks (such as SQLAlchemy) which will be available very soon.
Make sure to add port mapping for 8080 (this is default
configured with) when running Omnigres via Docker (using the same volume as
docker run --name omnigres \ -e POSTGRES_PASSWORD=omnigres \ -e POSTGRES_USER=omnigres \ -e POSTGRES_DB=omnigres \ --mount source=omnigres,target=/var/lib/postgresql/data -v $(pwd)/python-files:/python-files \ -p 127.0.0.1:5450:5432 -p 127.0.0.1:8000:8000 \ --rm ghcr.io/omnigres/omnigres-slim:latest
Setup HTTP handler for out Flask application:
You can hit the endpoints defined in the Flask code above.
Fetch all employees:
Create a new employee:
Fetch a particular employee: