-
Notifications
You must be signed in to change notification settings - Fork 0
Database Connection
Manages database sessions and CRUD operations via functions. These are in the file backend/database.py.
-
The database must be configured and working.
-
The
.streamlit/secrets.tomlfile must be configured with all the necessary data and credentials to access the database.
This project utilizes the following tools for database management:
- psycopg2: A PostgreSQL adapter for Python.
- SQLAlchemy: An ORM (Object-Relational Mapping) library for managing and interacting with database tables in Python.
- On the server startup, it reads database credentials (username, password, address, port, and name) from
.streamlit/secrets.tomland constructs the database URL for a PostgreSQL database. - Sets up a SQLAlchemy engine and the session factory.
- Loads initial users from
first_users.yamlif the database is empty. It automatically hashes their passwords, and adds them to the database.
To perform database queries, you must first obtain a database session using the get_db() context manager function from backend/database.py.
Note
The get_db() function is a generator that yields a database session object. It should be used within a with statement to ensure proper resource management.
Most functions that execute a database query require that a database session object must be passed as an argument.
with get_db() as db:
# Execute your database query hereThis file provides two generic functions:
-
add_to_db(): Adds aUser,VirtualMachine, orBookmarkobject to the database, commits the transaction, and refreshes the object. -
delete_from_db(): Removes aUser,VirtualMachine, orBookmarkobject from the database and commits the change.
with get_db() as db:
refreshed_VM = add_to_db(db, my_vm)
delete_from_db(db, refreshed_VM)- Official Streamlit Documentation
- Other Modules: