Create database module

This commit is contained in:
Em (Ethan) Ruszanowski 2024-02-26 17:56:06 -05:00
parent b2c392e5c5
commit 3eba2b644a
Signed by: em
GPG key ID: C725D6E571252B96
3 changed files with 28 additions and 1 deletions

5
app.py
View file

@ -1,4 +1,5 @@
import streamlit as st import streamlit as st
import database as db
class Moon: class Moon:
@ -92,9 +93,11 @@ def main():
st.markdown("# :red[Lethal Company] Scouter") st.markdown("# :red[Lethal Company] Scouter")
st.markdown(":rainbow[What does the scouter say about this moon's power level?]") st.markdown(":rainbow[What does the scouter say about this moon's power level?]")
moon_strings = db.get_moon_list()
moon = st.selectbox( moon = st.selectbox(
"Moon", "Moon",
sorted(m.name for m in moons), moon_strings,
placeholder="Moon! Pick a moon!", placeholder="Moon! Pick a moon!",
help="Pick your current moon.", help="Pick your current moon.",
) )

1
database/__init__.py Normal file
View file

@ -0,0 +1 @@
from .database import *

23
database/database.py Normal file
View file

@ -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