From ff1d50bc3b5e637b1ad3e54d3c42b01de04f6692 Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 6 Mar 2024 20:53:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E6=B8=B2=E6=9F=93=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db_utils.py | 33 +++++++++++++++++++++++++++++++++ flush.py | 27 +-------------------------- main.py | 27 +++++++++------------------ requirements.txt | 3 ++- 4 files changed, 45 insertions(+), 45 deletions(-) create mode 100644 db_utils.py 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