使用 python 将 AgGrid 数据保存到 sqlite
save the AgGrid data to sqlite with python
嗨,我正在构建一个应用程序来保存我们在工作中所做的项目的结果
我想用 aggrid 更新 table 它也会更新我正在使用的 sqlite 数据库
我设法在网络应用程序上进行编辑,但它不会更新数据库
如果有人能告诉我它会怎样很棒
这是我导入的库:
import streamlit as st
import pandas as pd
from streamlit_option_menu import option_menu
from st_aggrid import AgGrid,GridUpdateMode
from st_aggrid.grid_options_builder import GridOptionsBuilder
if selected == 'Editor':
def data_upload():
dfa = pd.read_sql('SELECT * FROM LightSpeed_Project',con=sqlite3.connect('Performance.db',))
return dfa
dfe = data_upload()
gd = GridOptionsBuilder.from_dataframe(dfe)
gd.configure_pagination(enabled=True)
gd.configure_default_column(editable=True, groupable=True)
sel_mode = st.radio('Selection Type', options=['single', 'multiple'])
gd.configure_selection(selection_mode=sel_mode, use_checkbox=True)
gridoptions = gd.build()
grid_table = AgGrid(dfe, gridOptions=gridoptions,
update_mode=GridUpdateMode.SELECTION_CHANGED,
height=500,
allow_unsafe_jscode=True,
# enable_enterprise_modules = True,
theme='fresh')
sel_row = grid_table["selected_rows"]
st.subheader("Output")
st.write(sel_row)
这是一个示例代码。请注意,我在更新模式下添加了 GridUpdateMode.VALUE_CHANGED
以在值更改时查看 grid_table 中的更改。还添加了一个按钮,用于根据 AgGrid 返回的更改更新数据库。
代码
import streamlit as st
import sqlite3
import pandas as pd
from streamlit_option_menu import option_menu
from st_aggrid import AgGrid,GridUpdateMode
from st_aggrid.grid_options_builder import GridOptionsBuilder
conn = sqlite3.connect('stocks.db')
cur = conn.cursor()
def create_table():
cur.execute('CREATE TABLE IF NOT EXISTS product(id integer PRIMARY KEY, name TEXT, count integer)')
def add(name, count):
cur.execute('INSERT INTO product(name, count) VALUES (?,?)', (name, count))
conn.commit()
def update(id, count):
cur.execute('UPDATE product SET count=? WHERE id=?', (count, id))
conn.commit()
create_table()
# Run once to add data to table.
# add('cpu', 5)
# add('psu', 5)
# add('mother board', 8)
# add('gpu', 10)
st.write('##### Intital contents of db')
df = pd.read_sql('SELECT * FROM product', con=conn)
st.write(df)
gd = GridOptionsBuilder.from_dataframe(df)
gd.configure_pagination(enabled=True)
gd.configure_default_column(editable=True, groupable=True)
sel_mode = st.radio('Selection Type', options=['single', 'multiple'])
gd.configure_selection(selection_mode=sel_mode, use_checkbox=True)
gridoptions = gd.build()
grid_table = AgGrid(df, gridOptions=gridoptions,
update_mode=GridUpdateMode.SELECTION_CHANGED | GridUpdateMode.VALUE_CHANGED,
height=500,
allow_unsafe_jscode=True,
# enable_enterprise_modules = True,
theme='fresh')
sel_row = grid_table["selected_rows"]
st.subheader("Output")
st.write(sel_row)
df_selected = pd.DataFrame(sel_row)
if st.button('Update db', key=1):
for i, r in df_selected.iterrows():
id = r['id']
cnt = r['count']
update(id, cnt)
st.write('##### Updated db')
df_update = pd.read_sql('SELECT * FROM product', con=conn)
st.write(df_update)
cur.close()
conn.close()
输出
嗨,我正在构建一个应用程序来保存我们在工作中所做的项目的结果 我想用 aggrid 更新 table 它也会更新我正在使用的 sqlite 数据库 我设法在网络应用程序上进行编辑,但它不会更新数据库 如果有人能告诉我它会怎样很棒 这是我导入的库:
import streamlit as st
import pandas as pd
from streamlit_option_menu import option_menu
from st_aggrid import AgGrid,GridUpdateMode
from st_aggrid.grid_options_builder import GridOptionsBuilder
if selected == 'Editor':
def data_upload():
dfa = pd.read_sql('SELECT * FROM LightSpeed_Project',con=sqlite3.connect('Performance.db',))
return dfa
dfe = data_upload()
gd = GridOptionsBuilder.from_dataframe(dfe)
gd.configure_pagination(enabled=True)
gd.configure_default_column(editable=True, groupable=True)
sel_mode = st.radio('Selection Type', options=['single', 'multiple'])
gd.configure_selection(selection_mode=sel_mode, use_checkbox=True)
gridoptions = gd.build()
grid_table = AgGrid(dfe, gridOptions=gridoptions,
update_mode=GridUpdateMode.SELECTION_CHANGED,
height=500,
allow_unsafe_jscode=True,
# enable_enterprise_modules = True,
theme='fresh')
sel_row = grid_table["selected_rows"]
st.subheader("Output")
st.write(sel_row)
这是一个示例代码。请注意,我在更新模式下添加了 GridUpdateMode.VALUE_CHANGED
以在值更改时查看 grid_table 中的更改。还添加了一个按钮,用于根据 AgGrid 返回的更改更新数据库。
代码
import streamlit as st
import sqlite3
import pandas as pd
from streamlit_option_menu import option_menu
from st_aggrid import AgGrid,GridUpdateMode
from st_aggrid.grid_options_builder import GridOptionsBuilder
conn = sqlite3.connect('stocks.db')
cur = conn.cursor()
def create_table():
cur.execute('CREATE TABLE IF NOT EXISTS product(id integer PRIMARY KEY, name TEXT, count integer)')
def add(name, count):
cur.execute('INSERT INTO product(name, count) VALUES (?,?)', (name, count))
conn.commit()
def update(id, count):
cur.execute('UPDATE product SET count=? WHERE id=?', (count, id))
conn.commit()
create_table()
# Run once to add data to table.
# add('cpu', 5)
# add('psu', 5)
# add('mother board', 8)
# add('gpu', 10)
st.write('##### Intital contents of db')
df = pd.read_sql('SELECT * FROM product', con=conn)
st.write(df)
gd = GridOptionsBuilder.from_dataframe(df)
gd.configure_pagination(enabled=True)
gd.configure_default_column(editable=True, groupable=True)
sel_mode = st.radio('Selection Type', options=['single', 'multiple'])
gd.configure_selection(selection_mode=sel_mode, use_checkbox=True)
gridoptions = gd.build()
grid_table = AgGrid(df, gridOptions=gridoptions,
update_mode=GridUpdateMode.SELECTION_CHANGED | GridUpdateMode.VALUE_CHANGED,
height=500,
allow_unsafe_jscode=True,
# enable_enterprise_modules = True,
theme='fresh')
sel_row = grid_table["selected_rows"]
st.subheader("Output")
st.write(sel_row)
df_selected = pd.DataFrame(sel_row)
if st.button('Update db', key=1):
for i, r in df_selected.iterrows():
id = r['id']
cnt = r['count']
update(id, cnt)
st.write('##### Updated db')
df_update = pd.read_sql('SELECT * FROM product', con=conn)
st.write(df_update)
cur.close()
conn.close()