Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,10 +4,116 @@ import numpy as np
|
|
4 |
import altair as alt
|
5 |
import pydeck as pdk
|
6 |
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
|
|
|
|
11 |
|
12 |
|
13 |
|
|
|
4 |
import altair as alt
|
5 |
import pydeck as pdk
|
6 |
import random
|
7 |
+
from pytz import country_names
|
8 |
+
from st_aggrid import AgGrid, GridUpdateMode, JsCode
|
9 |
+
from st_aggrid.grid_options_builder import GridOptionsBuilder
|
10 |
+
import snowflake.connector
|
11 |
+
from snowflake.connector.pandas_tools import write_pandas
|
12 |
+
from snowflake.connector import connect
|
13 |
|
14 |
|
15 |
|
16 |
+
@st.experimental_memo
|
17 |
+
def load_data():
|
18 |
+
df = pd.read_csv("CSV_samples/country-list.csv")
|
19 |
+
return df
|
20 |
+
|
21 |
+
|
22 |
+
@st.experimental_memo
|
23 |
+
def convert_df(df):
|
24 |
+
# IMPORTANT: Cache the conversion to prevent computation on every rerun
|
25 |
+
return df.to_csv().encode("utf-8")
|
26 |
+
|
27 |
+
|
28 |
+
def execute_query(conn, df_sel_row, table_name):
|
29 |
+
if not df_sel_row.empty:
|
30 |
+
conn.cursor().execute(
|
31 |
+
"CREATE OR REPLACE TABLE "
|
32 |
+
f"{table_name}(COUNTRY string, CAPITAL string, TYPE string)"
|
33 |
+
)
|
34 |
+
write_pandas(
|
35 |
+
conn=conn,
|
36 |
+
df=df_sel_row,
|
37 |
+
table_name=table_name,
|
38 |
+
database="STREAMLIT_DB",
|
39 |
+
schema="PUBLIC",
|
40 |
+
quote_identifiers=False,
|
41 |
+
)
|
42 |
+
|
43 |
+
|
44 |
+
# Initialize connection.
|
45 |
+
# Uses st.experimental_singleton to only run once.
|
46 |
+
@st.experimental_singleton
|
47 |
+
def init_connection():
|
48 |
+
return snowflake.connector.connect(**st.secrets["snowflake"])
|
49 |
+
|
50 |
+
|
51 |
+
# The code below is for the title and logo.
|
52 |
+
st.set_page_config(page_title="Dataframe with editable cells", page_icon="💾")
|
53 |
+
st.image(
|
54 |
+
"https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/240/apple/325/floppy-disk_1f4be.png",
|
55 |
+
width=100,
|
56 |
+
)
|
57 |
+
conn = init_connection()
|
58 |
+
df = load_data()
|
59 |
+
st.title("Dataframe with editable cells")
|
60 |
+
st.write("")
|
61 |
+
st.markdown(
|
62 |
+
"""This is a demo of a dataframe with editable cells, powered by
|
63 |
+
[streamlit-aggrid](https://pypi.org/project/streamlit-aggrid/).
|
64 |
+
You can edit the cells by clicking on them, and then export
|
65 |
+
your selection to a `.csv` file (or send it to your Snowflake DB!)"""
|
66 |
+
)
|
67 |
+
st.write("")
|
68 |
+
st.write("")
|
69 |
+
st.subheader("① Select and edit cells")
|
70 |
+
st.info("💡 Hold the `Shift` (⇧) key to select multiple rows at once.")
|
71 |
+
st.caption("")
|
72 |
+
gd = GridOptionsBuilder.from_dataframe(df)
|
73 |
+
gd.configure_pagination(enabled=True)
|
74 |
+
gd.configure_default_column(editable=True, groupable=True)
|
75 |
+
gd.configure_selection(selection_mode="multiple", use_checkbox=True)
|
76 |
+
gridoptions = gd.build()
|
77 |
+
grid_table = AgGrid(
|
78 |
+
df,
|
79 |
+
gridOptions=gridoptions,
|
80 |
+
update_mode=GridUpdateMode.SELECTION_CHANGED,
|
81 |
+
theme="material",
|
82 |
+
)
|
83 |
+
sel_row = grid_table["selected_rows"]
|
84 |
+
|
85 |
+
st.subheader(" ② Check your selection")
|
86 |
+
|
87 |
+
st.write("")
|
88 |
+
|
89 |
+
df_sel_row = pd.DataFrame(sel_row)
|
90 |
+
csv = convert_df(df_sel_row)
|
91 |
+
if not df_sel_row.empty:
|
92 |
+
st.write(df_sel_row)
|
93 |
+
st.download_button(
|
94 |
+
label="Download to CSV",
|
95 |
+
data=csv,
|
96 |
+
file_name="results.csv",
|
97 |
+
mime="text/csv",
|
98 |
+
)
|
99 |
+
st.write("")
|
100 |
+
st.write("")
|
101 |
+
|
102 |
+
st.subheader("③ Send to Snowflake DB ❄️")
|
103 |
+
|
104 |
+
st.write("")
|
105 |
+
table_name = st.text_input("Pick a table name", "YOUR_TABLE_NAME_HERE", help="No spaces allowed")
|
106 |
+
run_query = st.button(
|
107 |
+
"Add to DB", on_click=execute_query, args=(conn, df_sel_row, table_name)
|
108 |
+
)
|
109 |
+
if run_query and not df_sel_row.empty:
|
110 |
+
st.success(
|
111 |
+
f"✔️ Selection added to the `{table_name}` table located in the `STREAMLIT_DB` database."
|
112 |
+
)
|
113 |
+
st.snow()
|
114 |
|
115 |
+
if run_query and df_sel_row.empty:
|
116 |
+
st.info("Nothing to add to DB, please select some rows")
|
117 |
|
118 |
|
119 |
|