You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Generalise how line magic elements are handled and then add support for passing in py variables that can be used in the sql queries as template parameters.
The text was updated successfully, but these errors were encountered:
psychemedia
changed the title
Add magic arguments ro sipport template queries
Add magic arguments to pass variables into templated queries
Aug 24, 2024
Currently, the pglite_df_insert can accept a pandas.Dataframe which it will try insert into a specified table. The datarame value is picked up from the local_ns via local_ns[args.dataframe].
@line_magic@needs_local_scope@magic_arguments()@argument("-t", "--table", type=str, help="Table name")@argument("-d", "--dataframe", type=str, help="Datatframe")@argument("-w", "--widget-name", type=str, help="widget variable name")defpglite_df_insert(self, line, local_ns=None):
"""Call as: %pglite -d df -t tableName"""frompandasimportDataFrameargs=parse_argstring(self.pglite_df_insert, line)
ifargs.dataframenotinlocal_ns:
raiseValueError(f"DataFrame '{args.dataframe}' not found in the local namespace")
# Get the DataFramedf=local_ns[args.dataframe]
# Validate the DataFrameifnotisinstance(df, DataFrame):
raiseTypeError(f"'{args.dataframe}' is not a pandas DataFrame")
# Generate the SQL statementcolumns=', '.join(df.columns)
values=',\n '.join(
[f"({', '.join(repr(value) forvalueinrow)})"forrowindf.itertuples(index=False, name=None)]
)
sql=f"INSERT INTO {args.table} ({columns})\nVALUES\n{values};"returnself._run_query(args, sql)
Generalise how line magic elements are handled and then add support for passing in py variables that can be used in the sql queries as template parameters.
The text was updated successfully, but these errors were encountered: