Archon commited on
Commit
5e27e41
·
1 Parent(s): 89299b3

add: st.column_config helper

Browse files
README.md CHANGED
@@ -1 +1,14 @@
1
  # streamlit-parent
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # streamlit-parent
2
+
3
+ ## Installation
4
+
5
+ ```sh
6
+ python -m venv .venv
7
+ . .venv/bin/activate
8
+
9
+ pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple
10
+
11
+ streamlit run Home.py
12
+ ```
13
+
14
+ 打开浏览器,访问: http://127.0.0.1:8051
models/__init__.py CHANGED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from sqlalchemy import String, Date, Integer, Double
3
+ from sqlalchemy.orm.attributes import InstrumentedAttribute
4
+
5
+
6
+ class StreamlitHelper:
7
+ @classmethod
8
+ def get_column_configurations(cls):
9
+ config = {}
10
+ for k in cls.__dict__:
11
+ v = cls.__dict__[k]
12
+ if isinstance(v, InstrumentedAttribute):
13
+ config.update(get_column_configuration(v))
14
+ return config
15
+
16
+
17
+ def get_column_configuration(column):
18
+ comment: str = column.comment
19
+ disabled = True
20
+ if comment and "可修改" in comment:
21
+ disabled = False
22
+ if isinstance(column.type, String):
23
+ return {
24
+ column.name: st.column_config.TextColumn(disabled=disabled, help=comment)
25
+ }
26
+ elif isinstance(column.type, Integer) or isinstance(column.type, Double):
27
+ return {
28
+ column.name: st.column_config.NumberColumn(disabled=disabled, help=comment)
29
+ }
30
+ elif isinstance(column.type, Date):
31
+ return {
32
+ column.name: st.column_config.DateColumn(disabled=disabled, help=comment)
33
+ }
34
+ else:
35
+ return {
36
+ column.name: st.column_config.TextColumn(disabled=disabled, help=comment)
37
+ }
models/t_smi_rank_list_source.py CHANGED
@@ -5,20 +5,18 @@ from sqlalchemy import Column, String, BigInteger
5
  from sqlalchemy.orm import Session
6
 
7
  from database import Base
 
8
 
9
 
10
- class TSmiRankListSource(Base):
11
  __tablename__: str = "t_smi_rank_list_source"
12
 
13
  item_id = Column(
14
- String(64),
15
- primary_key=True,
16
- index=True,
17
- default=uuid.uuid4().hex,
18
  )
19
- brand_id = Column(BigInteger, nullable=False, default="0")
20
- brand_name = Column(String(64), nullable=True, default="0")
21
- brand_es_name = Column(String(64), nullable=True, default="0")
22
 
23
 
24
  def init_mock_data(db: Session, size: int = 5) -> None:
 
5
  from sqlalchemy.orm import Session
6
 
7
  from database import Base
8
+ from models import StreamlitHelper
9
 
10
 
11
+ class TSmiRankListSource(Base, StreamlitHelper):
12
  __tablename__: str = "t_smi_rank_list_source"
13
 
14
  item_id = Column(
15
+ String(64), primary_key=True, comment="品牌id,榜单id,发表时间,站点id,阵地id的md5值"
 
 
 
16
  )
17
+ brand_id = Column(BigInteger, nullable=False, comment="品牌id 展现 可修改")
18
+ brand_name = Column(String(64), comment="品牌名称 展现 可修改")
19
+ brand_es_name = Column(String(64), comment="在ES中的品牌名称 展现 可修改")
20
 
21
 
22
  def init_mock_data(db: Session, size: int = 5) -> None:
pages/data_editor_sqlite.py CHANGED
@@ -30,29 +30,6 @@ def save_album(db: Session, df: pd.DataFrame) -> None:
30
  st.experimental_rerun()
31
 
32
 
33
- def get_column_configuration():
34
- return {
35
- storage.TSmiRankListSource.item_id.name: st.column_config.TextColumn(
36
- max_chars=64,
37
- disabled=True,
38
- ),
39
- storage.TSmiRankListSource.brand_id.name: st.column_config.NumberColumn(
40
- default=0,
41
- disabled=True,
42
- ),
43
- storage.TSmiRankListSource.brand_name.name: st.column_config.TextColumn(
44
- max_chars=64,
45
- default="0",
46
- disabled=False,
47
- ),
48
- storage.TSmiRankListSource.brand_es_name.name: st.column_config.TextColumn(
49
- max_chars=64,
50
- default="0",
51
- disabled=False,
52
- ),
53
- }
54
-
55
-
56
  # page
57
  st.set_page_config("Data Editor on Databases", "🏦")
58
 
@@ -62,7 +39,7 @@ with next(get_db()) as db:
62
  edited_data = table.data_editor(
63
  storage.list_records_as_dataframe(db),
64
  key=TABLE_CHANGED_KEY,
65
- column_config=get_column_configuration(),
66
  use_container_width=True,
67
  hide_index=True,
68
  num_rows="fixed",
 
30
  st.experimental_rerun()
31
 
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  # page
34
  st.set_page_config("Data Editor on Databases", "🏦")
35
 
 
39
  edited_data = table.data_editor(
40
  storage.list_records_as_dataframe(db),
41
  key=TABLE_CHANGED_KEY,
42
+ column_config=storage.TSmiRankListSource.get_column_configurations(),
43
  use_container_width=True,
44
  hide_index=True,
45
  num_rows="fixed",
resource/database.sqlite CHANGED
Binary files a/resource/database.sqlite and b/resource/database.sqlite differ