Hi all,
I have a problem with getting JSON with changes from data editor. I have set the ID column to be the index in dataframe, but this seems to be ignored from json dump from session state as it uses default index.
Easeier to explain with an example –
Table is as follows:
CREATE OR REPLACE TABLE TESTTBL (ID NUMBER(38,0) NOT NULL unique AUTOINCREMENT start 1 increment 1, COL1 VARCHAR(500) NOT NULL, COL2 NUMBER(38,0), constraint TEST_PK primary key (ID));
INSERT INTO TESTTBL (COL1, COL2) VALUES('aaa', 123);
INSERT INTO TESTTBL (COL1, COL2) VALUES('bbb', 456);
Here is the Streamlit code:
import streamlit as st
import pandas as pd
import json
from snowflake.snowpark.context import get_active_session
st.title("Dude wheres my id :eyes:")
session = get_active_session()
df = session.sql("SELECT * FROM TESTTBL").collect()
df = pd.DataFrame(df)
df.set_index('ID', inplace=True)
st.data_editor(df, use_container_width=True, key="first", num_rows="dynamic")
if st.button('Show JSON'):
st.write(json.dumps(st.session_state["first"]))
After running this I edited one row and added one additional row, so table looks like this:
1 | aaa | 123 |
---|---|---|
2 | bbb test | 456 |
7 | ccc | 789 |
Resulting JSON will be {“edited_rows”: {“1”: {“COL1”: “bbb test”}}, “added_rows”: [{“_index”: 7, “COL1”: “ccc”, “COL2”: 789}], “deleted_rows”: [ ]}
where added row has a correct value for index, but edited row has 1 as index which is not correct, as it should be 2.
Does anyone have an idea what is going on with this and how to get correct index values in JSON dumps?