From 3eba2b644a79f6ad3c1f8367c41456f5855a4d5a Mon Sep 17 00:00:00 2001 From: Ethan Ruszanowski Date: Mon, 26 Feb 2024 17:56:06 -0500 Subject: [PATCH] Create database module --- app.py | 5 ++++- database/__init__.py | 1 + database/database.py | 23 +++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 database/__init__.py create mode 100644 database/database.py diff --git a/app.py b/app.py index f34b1c0..eb3013e 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,5 @@ import streamlit as st +import database as db class Moon: @@ -92,9 +93,11 @@ def main(): st.markdown("# :red[Lethal Company] Scouter") st.markdown(":rainbow[What does the scouter say about this moon's power level?]") + moon_strings = db.get_moon_list() + moon = st.selectbox( "Moon", - sorted(m.name for m in moons), + moon_strings, placeholder="Moon! Pick a moon!", help="Pick your current moon.", ) diff --git a/database/__init__.py b/database/__init__.py new file mode 100644 index 0000000..89578ee --- /dev/null +++ b/database/__init__.py @@ -0,0 +1 @@ +from .database import * diff --git a/database/database.py b/database/database.py new file mode 100644 index 0000000..2cb1c82 --- /dev/null +++ b/database/database.py @@ -0,0 +1,23 @@ +import os +import sqlite3 + + +def get_connection() -> sqlite3.Connection: + if os.getenv("DATABASE_FILE"): + return sqlite3.connect(os.getenv("DATABASE_FILE")) + else: + return sqlite3.connect("./scouter.db") + + +def get_moon_list() -> list[str] | None: + with get_connection() as connection: + cursor = connection.cursor() + moons = cursor.execute( + "select moon_name from moon order by moon_id" + ) + + if moons: + moons = [moon[0] for moon in moons] + return moons + else: + return None