MCPcopy
hub / github.com/man-group/dtale / build_code_export

Function build_code_export

dtale/code_export.py:45–130  ·  view source on GitHub ↗

Helper function for building a string representing the code that was run to get the data you are viewing to that point. :param data_id: integer string identifier for a D-Tale process's data :type data_id: str :param imports: string representing the imports at the top of the cod

(data_id, imports="import pandas as pd\n\n", query=None)

Source from the content-addressed store, hash-verified

43
44
45def build_code_export(data_id, imports="import pandas as pd\n\n", query=None):
46 """
47 Helper function for building a string representing the code that was run to get the data you are viewing to that
48 point.
49
50 :param data_id: integer string identifier for a D-Tale process's data
51 :type data_id: str
52 :param imports: string representing the imports at the top of the code string
53 :type imports: string, optional
54 :param query: pandas dataframe query string
55 :type query: str, optional
56 :return: python code string
57 """
58 history = global_state.get_history(data_id) or []
59 settings = global_state.get_settings(data_id) or {}
60 ctxt_vars = global_state.get_context_variables(data_id)
61
62 startup_code = settings.get("startup_code") or ""
63 if startup_code and not startup_code.endswith("\n"):
64 startup_code += "\n"
65 xarray_setup = ""
66 if global_state.get_dataset(data_id) is not None:
67 xarray_dims = global_state.get_dataset_dim(data_id)
68 if len(xarray_dims):
69 xarray_setup = (
70 "df = ds.sel({selectors}).to_dataframe()\n"
71 "df = df.reset_index().drop('index', axis=1, errors='ignore')\n"
72 "df = df.set_index(list(ds.dims.keys()))\n"
73 ).format(
74 selectors=", ".join(
75 "{}='{}'".format(k, v) for k, v in xarray_dims.items()
76 )
77 )
78 else:
79 xarray_setup = (
80 "df = ds.to_dataframe()\n"
81 "df = df.reset_index().drop('index', axis=1, errors='ignore')\n"
82 "df = df.set_index(list(ds.dims.keys()))\n"
83 )
84 startup_str = (
85 "# DISCLAIMER: 'df' refers to the data you passed in when calling 'dtale.show'\n\n"
86 "{imports}"
87 "{xarray_setup}"
88 "{startup}"
89 "if isinstance(df, (pd.DatetimeIndex, pd.MultiIndex)):\n"
90 "\tdf = df.to_frame(index=False)\n\n"
91 "# remove any pre-existing indices for ease of use in the D-Tale code, but this is not required\n"
92 "df = df.reset_index().drop('index', axis=1, errors='ignore')\n"
93 "df.columns = [str(c) for c in df.columns] # update columns to strings in case they are numbers\n"
94 ).format(imports=imports, xarray_setup=xarray_setup, startup=startup_code)
95 final_history = [startup_str] + history
96 final_query = query
97 if final_query is None:
98 final_query = build_query(data_id, global_state.get_query(data_id))
99
100 if final_query is not None and final_query != "":
101 if len(ctxt_vars or {}):
102 final_history.append(

Callers 15

__init__Method · 0.90
buildMethod · 0.90
describeFunction · 0.90
get_correlations_tsFunction · 0.90
get_scatterFunction · 0.90
get_code_exportFunction · 0.90
build_matrixFunction · 0.90
heatmap_builderFunction · 0.90
candlestick_builderFunction · 0.90
map_builderFunction · 0.90
build_figure_dataFunction · 0.90

Calls 10

build_queryFunction · 0.90
triple_quoteFunction · 0.90
get_historyMethod · 0.80
get_context_variablesMethod · 0.80
get_datasetMethod · 0.80
get_dataset_dimMethod · 0.80
get_queryMethod · 0.80
get_settingsMethod · 0.45
getMethod · 0.45
itemsMethod · 0.45