lhoestq HF staff commited on
Commit
bd73664
·
1 Parent(s): 9d9f5bf
Files changed (1) hide show
  1. app.py +13 -4
app.py CHANGED
@@ -44,8 +44,12 @@ def to_json_df(con: Connection, tbl: Table) -> pd.DataFrame:
44
 
45
  def from_json_df(con: Connection, df: pd.DataFrame, columns: list[str], dtypes: list[Dtype]) -> Table:
46
  query = ", ".join(
47
- "if(" + col + " IS null, null, (" + col + "::JSON::" + str(dtype) + ")"
48
- + ("[2:-2]" if str(dtype) == "VARCHAR" else "") # remove double quotes at the start and end
 
 
 
 
49
  + ") AS " + col for col, dtype in zip(columns, dtypes))
50
  return con.sql(f"SELECT {query} FROM df")
51
 
@@ -139,7 +143,7 @@ with gr.Blocks(css=css) as demo:
139
  )
140
  }
141
 
142
- @dataset_dropdown.select(inputs=[dataset_dropdown], outputs=[session_state, loading_codes_json, subset_dropdown, split_dropdown, dataframe])
143
  def _show_subset_dropdown(session: str | None, dataset: str):
144
  subsets, loading_codes = show_subset_dropdown(dataset)
145
  splits = show_split_dropdown(subsets["value"], loading_codes)
@@ -185,7 +189,12 @@ with gr.Blocks(css=css) as demo:
185
  dtypes = empty_dataset_tbl.dtypes
186
  tbl = from_json_df(con, df, columns=columns, dtypes=dtypes)
187
  # TODO add edits for page > 1
188
- con.sql(f"INSERT OR REPLACE INTO edits SELECT * FROM (SELECT unnest(range({len(df)})) AS rowid) POSITIONAL JOIN tbl")
 
 
 
 
 
189
  print(f"Saved {dataset} edits")
190
 
191
 
 
44
 
45
  def from_json_df(con: Connection, df: pd.DataFrame, columns: list[str], dtypes: list[Dtype]) -> Table:
46
  query = ", ".join(
47
+ "if(" + col + " IS null, null, "
48
+ + (
49
+ "trim(" + col + """::JSON::VARCHAR, '"')"""
50
+ if str(dtype) == "VARCHAR"
51
+ else "(" + col + "::JSON::" + str(dtype) + ")"
52
+ ) # remove double quotes at the start and end
53
  + ") AS " + col for col, dtype in zip(columns, dtypes))
54
  return con.sql(f"SELECT {query} FROM df")
55
 
 
143
  )
144
  }
145
 
146
+ @dataset_dropdown.select(inputs=[session_state, dataset_dropdown], outputs=[session_state, loading_codes_json, subset_dropdown, split_dropdown, dataframe])
147
  def _show_subset_dropdown(session: str | None, dataset: str):
148
  subsets, loading_codes = show_subset_dropdown(dataset)
149
  splits = show_split_dropdown(subsets["value"], loading_codes)
 
189
  dtypes = empty_dataset_tbl.dtypes
190
  tbl = from_json_df(con, df, columns=columns, dtypes=dtypes)
191
  # TODO add edits for page > 1
192
+ # Note: Here we don't use INSERT OR REPLACE because of Not implemented Error: List Update is not supported.
193
+ con.sql(f"DELETE FROM edits WHERE rowid IN range({len(df)})")
194
+ try:
195
+ con.sql(f"INSERT INTO edits SELECT * FROM (SELECT unnest(range({len(df)})) AS rowid) POSITIONAL JOIN tbl")
196
+ except duckdb.ConversionException as e:
197
+ raise gr.Error(str(e).split('\n')[0], title="duckdb.ConversionException")
198
  print(f"Saved {dataset} edits")
199
 
200