def build_code_export(data_id, imports="import pandas as pd\n\n", query=None): """ 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 code string :type imports: string, optional :param query: pandas dataframe query string :type query: str, optional :return: python code string """ history = global_state.get_history(data_id) or [] settings = global_state.get_settings(data_id) or {} ctxt_vars = global_state.get_context_variables(data_id) startup_code = settings.get("startup_code") or "" if startup_code and not startup_code.endswith("\n"): startup_code += "\n" xarray_setup = "" if data_id in global_state.DATASETS: xarray_dims = global_state.get_dataset_dim(data_id) if len(xarray_dims): xarray_setup = ( "df = ds.sel({selectors}).to_dataframe()\n" "df = df.reset_index().drop('index', axis=1, errors='ignore')\n" "df = df.set_index(list(ds.dims.keys()))\n" ).format( selectors=", ".join( "{}='{}'".format(k, v) for k, v in xarray_dims.items() ) ) else: xarray_setup = ( "df = ds.to_dataframe()\n" "df = df.reset_index().drop('index', axis=1, errors='ignore')\n" "df = df.set_index(list(ds.dims.keys()))\n" ) startup_str = ( "# DISCLAIMER: 'df' refers to the data you passed in when calling 'dtale.show'\n\n" "{imports}" "{xarray_setup}" "{startup}" "if isinstance(df, (pd.DatetimeIndex, pd.MultiIndex)):\n" "\tdf = df.to_frame(index=False)\n\n" "# remove any pre-existing indices for ease of use in the D-Tale code, but this is not required\n" "df = df.reset_index().drop('index', axis=1, errors='ignore')\n" "df.columns = [str(c) for c in df.columns] # update columns to strings in case they are numbers\n" ).format(imports=imports, xarray_setup=xarray_setup, startup=startup_code) final_history = [startup_str] + history final_query = query if final_query is None: final_query = settings.get("query") if final_query is not None and final_query != "": if len(ctxt_vars or {}): final_history.append( ( "\n# this is injecting any context variables you may have passed into 'dtale.show'\n" "import dtale.global_state as dtale_global_state\n" "\n# DISCLAIMER: running this line in a different process than the one it originated will produce\n" "# differing results\n" "ctxt_vars = dtale_global_state.get_context_variables('{data_id}')\n\n" "df = df.query({query}, local_dict=ctxt_vars)\n" ).format(query=triple_quote(final_query), data_id=data_id) ) else: final_history.append( "df = df.query({})\n".format(triple_quote(final_query)) ) elif settings.get("query"): final_history.append( "df = df.query({})\n".format(triple_quote(settings["query"])) ) if "sort" in settings: cols, dirs = [], [] for col, dir in settings["sort"]: cols.append(col) dirs.append("True" if dir == "ASC" else "False") final_history.append( "df = df.sort_values(['{cols}'], ascending=[{dirs}])\n".format( cols=", ".join(cols), dirs="', '".join(dirs) ) ) return final_history
def test_view(unittest): from dtale.views import startup import dtale.global_state as global_state global_state.clear_store() with app.test_client() as c: global_state.new_data_inst(c.port) startup(URL, data=xarray_data(), data_id=c.port) assert global_state.get_dataset(c.port) is not None response = c.get("/dtale/main/{}".format(c.port)) assert 'input id="xarray" value="True"' not in str(response.data) assert 'input id="xarray_dim" value="{}"' not in str(response.data) resp = c.get("/dtale/code-export/{}".format(c.port)) assert resp.status_code == 200 response_data = resp.json assert response_data["success"] resp = c.get("/dtale/xarray-coordinates/{}".format(c.port)) response_data = resp.json expected = [ { "count": 3, "dtype": "str64" if PY3 else "string16", "name": "location", }, { "count": 731, "dtype": "datetime64[ns]", "name": "time" }, ] unittest.assertEqual( sorted(response_data["data"], key=lambda c: c["name"]), expected) resp = c.get("/dtale/xarray-dimension-values/{}/location".format( c.port)) response_data = resp.json unittest.assertEqual( response_data["data"], [{ "value": "IA" }, { "value": "IN" }, { "value": "IL" }], ) resp = c.get( "/dtale/update-xarray-selection/{}".format(c.port), query_string=dict(selection=json.dumps(dict(location="IA"))), ) assert resp.status_code == 200 assert list(global_state.get_data(c.port).location.unique()) == ["IA"] assert global_state.get_dataset_dim(c.port)["location"] == "IA" resp = c.get( "/dtale/update-xarray-selection/{}".format(c.port), query_string=dict(selection=json.dumps(dict())), ) assert resp.status_code == 200 assert list(global_state.get_data(c.port).location.unique()) == [ "IA", "IN", "IL", ] resp = c.get("/dtale/code-export/{}".format(c.port)) assert resp.status_code == 200 response_data = resp.json assert response_data["success"] with app.test_client() as c: zero_dim_xarray = xarray_data().sel(location="IA", time="2000-01-01") startup(URL, data=zero_dim_xarray, data_id=c.port) assert global_state.get_dataset(c.port) is not None response = c.get("/dtale/main/{}".format(c.port)) assert 'input id="xarray" value="True"' not in str(response.data) assert 'input id="xarray_dim" value="{}"' not in str(response.data)