diff --git a/db_utils.py b/db_utils.py new file mode 100644 index 0000000..8ce0a17 --- /dev/null +++ b/db_utils.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +# -*- coding:utf-8 -*- +from loguru import logger +from sqlalchemy import Column, Integer, String, create_engine +from sqlalchemy.orm import declarative_base, sessionmaker + +from bak.evolve_config2 import train_data +from bak.init_data import BatchDataBase + +Base = declarative_base() +# 创建 SQLite 数据库引擎 +engine = create_engine('sqlite:///data.db', echo=False) + + +class BatchData(Base): + __tablename__ = 'batch_data' + id = Column(Integer, primary_key=True, autoincrement=True) + year = Column(Integer) + census_batch = Column(String) # 普查批次 + id_code = Column(String) + precision = Column(String) # 精度 + is_train = Column(Integer) + is_validation = Column(Integer) + ann_file = Column(String) + img_prefix = Column(String) + filter_empty_gt = Column(Integer) + update_cache = Column(Integer) + + +Base.metadata.create_all(engine) + +Session = sessionmaker(bind=engine, ) +session = Session() \ No newline at end of file diff --git a/flush.py b/flush.py index 75b8369..ef63f8f 100644 --- a/flush.py +++ b/flush.py @@ -1,36 +1,11 @@ #!/usr/bin/env python3 # -*- coding:utf-8 -*- from loguru import logger -from sqlalchemy import Column, Integer, String, create_engine -from sqlalchemy.orm import declarative_base, sessionmaker from bak.evolve_config2 import train_data from bak.init_data import BatchDataBase +from db_utils import BatchData, session -Base = declarative_base() -# 创建 SQLite 数据库引擎 -engine = create_engine('sqlite:///data.db', echo=False) - - -class BatchData(Base): - __tablename__ = 'batch_data' - id = Column(Integer, primary_key=True, autoincrement=True) - year = Column(Integer) - census_batch = Column(String) # 普查批次 - id_code = Column(String) - precision = Column(String) # 精度 - is_train = Column(Integer) - is_validation = Column(Integer) - ann_file = Column(String) - img_prefix = Column(String) - filter_empty_gt = Column(Integer) - update_cache = Column(Integer) - - -Base.metadata.create_all(engine) - -Session = sessionmaker(bind=engine, ) -session = Session() logger.debug(f"{len(train_data)=}") census_batches = [] diff --git a/main.py b/main.py index dc87ef7..bb9b0f3 100644 --- a/main.py +++ b/main.py @@ -1,22 +1,13 @@ # streamlit_app.py - +import pandas as pd import streamlit as st -# Create the SQL connection to pets_db as specified in your secrets file. -conn = st.connection('data_db', type='sql') +from bak.init_data import BatchDataRead +from db_utils import BatchData, session -# Insert some data with conn.session. -with conn.session as s: - s.execute('CREATE TABLE IF NOT EXISTS pet_owners (person TEXT, pet TEXT);') - s.execute('DELETE FROM pet_owners;') - pet_owners = {'jerry': 'fish', 'barbara': 'cat', 'alex': 'puppy'} - for k in pet_owners: - s.execute( - 'INSERT INTO pet_owners (person, pet) VALUES (:owner, :pet);', - params=dict(owner=k, pet=pet_owners[k]) - ) - s.commit() - -# Query and display the data you inserted -pet_owners = conn.query('select * from pet_owners') -st.dataframe(pet_owners) +df = pd.DataFrame( + data=(BatchDataRead.from_orm(db_obj).dict() + for db_obj in session.query(BatchData).all()) +) +# +st.dataframe(df, use_container_width=True) diff --git a/requirements.txt b/requirements.txt index 0175e65..ab0affa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ sqlalchemy~=1.4 streamlit~=1.29 pydantic~=1.0 -loguru~=0.7 \ No newline at end of file +loguru~=0.7 +pandas~=2.2.1 \ No newline at end of file