Skip to content

Commit a96b877

Browse files
committed
Add browse auto_height option, restrict ipydatagrid version
1 parent 67715ec commit a96b877

File tree

3 files changed

+36
-77
lines changed

3 files changed

+36
-77
lines changed

nbs/07_browse.ipynb

+20-63
Original file line numberDiff line numberDiff line change
@@ -15,51 +15,6 @@
1515
"execution_count": null,
1616
"metadata": {},
1717
"outputs": [
18-
{
19-
"name": "stderr",
20-
"output_type": "stream",
21-
"text": [
22-
"[autoreload of fastcore.basics failed: Traceback (most recent call last):\n",
23-
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 276, in check\n",
24-
" superreload(m, reload, self.old_objects)\n",
25-
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 500, in superreload\n",
26-
" update_generic(old_obj, new_obj)\n",
27-
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 397, in update_generic\n",
28-
" update(a, b)\n",
29-
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 365, in update_class\n",
30-
" update_instances(old, new)\n",
31-
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 323, in update_instances\n",
32-
" object.__setattr__(ref, \"__class__\", new)\n",
33-
"TypeError: can't apply this __setattr__ to _InfMeta object\n",
34-
"]\n",
35-
"[autoreload of fastcore.foundation failed: Traceback (most recent call last):\n",
36-
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 276, in check\n",
37-
" superreload(m, reload, self.old_objects)\n",
38-
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 500, in superreload\n",
39-
" update_generic(old_obj, new_obj)\n",
40-
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 397, in update_generic\n",
41-
" update(a, b)\n",
42-
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 365, in update_class\n",
43-
" update_instances(old, new)\n",
44-
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 323, in update_instances\n",
45-
" object.__setattr__(ref, \"__class__\", new)\n",
46-
"TypeError: can't apply this __setattr__ to _L_Meta object\n",
47-
"]\n",
48-
"[autoreload of fastcore.meta failed: Traceback (most recent call last):\n",
49-
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 276, in check\n",
50-
" superreload(m, reload, self.old_objects)\n",
51-
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 500, in superreload\n",
52-
" update_generic(old_obj, new_obj)\n",
53-
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 397, in update_generic\n",
54-
" update(a, b)\n",
55-
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 365, in update_class\n",
56-
" update_instances(old, new)\n",
57-
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 323, in update_instances\n",
58-
" object.__setattr__(ref, \"__class__\", new)\n",
59-
"TypeError: can't apply this __setattr__ to PrePostInitMeta object\n",
60-
"]\n"
61-
]
62-
},
6318
{
6419
"name": "stdout",
6520
"output_type": "stream",
@@ -1332,7 +1287,7 @@
13321287
"outputs": [],
13331288
"source": [
13341289
"#| export\n",
1335-
"def display_df_as_ipydatagrid(df):\n",
1290+
"def display_df_as_ipydatagrid(df, auto_height=True):\n",
13361291
" from ipydatagrid import DataGrid, TextRenderer\n",
13371292
" i_renderer = TextRenderer(horizontal_alignment=\"right\", background_color=\"rgb(243, 243, 243)\")\n",
13381293
" d_renderer = TextRenderer(horizontal_alignment=\"right\")\n",
@@ -1354,18 +1309,20 @@
13541309
" 20 + char_px_width*max((len(str(value)) for value in temp_head[name])),\n",
13551310
" )\n",
13561311
" column_widths[\" \"] = 20 + char_px_width*len(str(max(df.index.values)))\n",
1357-
" g = DataGrid(df,\n",
1358-
" index_name=\" \",\n",
1359-
" editable=False,\n",
1360-
" selection_mode='cell',\n",
1361-
" base_row_size=21,\n",
1362-
" auto_fit_columns=False,\n",
1363-
" default_renderer=d_renderer,\n",
1364-
" header_renderer=h_renderer,\n",
1365-
" renderers={\" \": i_renderer},\n",
1366-
" column_widths=column_widths,\n",
1367-
" #layout={\"height\": \"100%\"},\n",
1368-
" )\n",
1312+
" g_kwargs = dict(\n",
1313+
" index_name=\" \",\n",
1314+
" editable=False,\n",
1315+
" selection_mode='cell',\n",
1316+
" base_row_size=21,\n",
1317+
" auto_fit_columns=False,\n",
1318+
" default_renderer=d_renderer,\n",
1319+
" header_renderer=h_renderer,\n",
1320+
" renderers={\" \": i_renderer},\n",
1321+
" column_widths=column_widths,\n",
1322+
" )\n",
1323+
" if auto_height:\n",
1324+
" g_kwargs['layout'] = {\"height\": \"100%\"}\n",
1325+
" g = DataGrid(df, **g_kwargs)\n",
13691326
" g.grid_style = {\n",
13701327
" \"background_color\": \"rgb(255, 255, 255)\",\n",
13711328
" \"header_background_color\": \"rgb(243, 243, 243)\",\n",
@@ -1411,12 +1368,12 @@
14111368
{
14121369
"data": {
14131370
"application/vnd.jupyter.widget-view+json": {
1414-
"model_id": "ce1e6d559d4f4c61b7afb35f179ebb55",
1371+
"model_id": "70a7b2e8bbdd43adb6c9344bb28dabfe",
14151372
"version_major": 2,
14161373
"version_minor": 0
14171374
},
14181375
"text/plain": [
1419-
"DataGrid(auto_fit_params={'area': 'all', 'padding': 30, 'numCols': None}, base_row_size=21, column_widths={'ye"
1376+
"DataGrid(auto_fit_params={'area': 'all', 'padding': 30, 'numCols': None}, base_row_size=21, column_widths={'ma"
14201377
]
14211378
},
14221379
"metadata": {},
@@ -1456,7 +1413,7 @@
14561413
{
14571414
"data": {
14581415
"application/vnd.jupyter.widget-view+json": {
1459-
"model_id": "c82cd1830aab494ea9f988d1a33e9889",
1416+
"model_id": "1c0294df9f164257aecd4e1225aa5c24",
14601417
"version_major": 2,
14611418
"version_minor": 0
14621419
},
@@ -1471,7 +1428,7 @@
14711428
"source": [
14721429
"#| eval: False\n",
14731430
"df = get_df(*browse_df_params('', obs_count(), '.'))\n",
1474-
"display_df_as_ipydatagrid(df)"
1431+
"display_df_as_ipydatagrid(df, False)"
14751432
]
14761433
},
14771434
{
@@ -1486,7 +1443,7 @@
14861443
"traceback": [
14871444
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
14881445
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
1489-
"Cell \u001b[1;32mIn[144], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m#| hide\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnbdev\u001b[39;00m; nbdev\u001b[38;5;241m.\u001b[39mnbdev_export()\n",
1446+
"Cell \u001b[1;32mIn[198], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m#| hide\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnbdev\u001b[39;00m; nbdev\u001b[38;5;241m.\u001b[39mnbdev_export()\n",
14901447
"File \u001b[1;32mc:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\fastcore\\script.py:112\u001b[0m, in \u001b[0;36mcall_parse.<locals>._f\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m 109\u001b[0m \u001b[38;5;129m@wraps\u001b[39m(func)\n\u001b[0;32m 110\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_f\u001b[39m(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[0;32m 111\u001b[0m mod \u001b[38;5;241m=\u001b[39m inspect\u001b[38;5;241m.\u001b[39mgetmodule(inspect\u001b[38;5;241m.\u001b[39mcurrentframe()\u001b[38;5;241m.\u001b[39mf_back)\n\u001b[1;32m--> 112\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m mod: \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 113\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m SCRIPT_INFO\u001b[38;5;241m.\u001b[39mfunc \u001b[38;5;129;01mand\u001b[39;00m mod\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;241m==\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m\"\u001b[39m: SCRIPT_INFO\u001b[38;5;241m.\u001b[39mfunc \u001b[38;5;241m=\u001b[39m func\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\n\u001b[0;32m 114\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(sys\u001b[38;5;241m.\u001b[39margv)\u001b[38;5;241m>\u001b[39m\u001b[38;5;241m1\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m sys\u001b[38;5;241m.\u001b[39margv[\u001b[38;5;241m1\u001b[39m]\u001b[38;5;241m==\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m'\u001b[39m: sys\u001b[38;5;241m.\u001b[39margv\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;241m1\u001b[39m)\n",
14911448
"File \u001b[1;32mc:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\nbdev\\doclinks.py:149\u001b[0m, in \u001b[0;36mnbdev_export\u001b[1;34m(path, procs, **kwargs)\u001b[0m\n\u001b[0;32m 147\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m procs:\n\u001b[0;32m 148\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnbdev\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mexport\u001b[39;00m\n\u001b[1;32m--> 149\u001b[0m procs \u001b[38;5;241m=\u001b[39m \u001b[43m[\u001b[49m\u001b[38;5;28;43mgetattr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mnbdev\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexport\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mp\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mp\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mL\u001b[49m\u001b[43m(\u001b[49m\u001b[43mprocs\u001b[49m\u001b[43m)\u001b[49m\u001b[43m]\u001b[49m\n\u001b[0;32m 150\u001b[0m files \u001b[38;5;241m=\u001b[39m nbglob(path\u001b[38;5;241m=\u001b[39mpath, as_path\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\u001b[38;5;241m.\u001b[39msorted(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mname\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 151\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m f \u001b[38;5;129;01min\u001b[39;00m files: nb_export(f, procs\u001b[38;5;241m=\u001b[39mprocs)\n",
14921449
"File \u001b[1;32mc:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\nbdev\\doclinks.py:149\u001b[0m, in \u001b[0;36m<listcomp>\u001b[1;34m(.0)\u001b[0m\n\u001b[0;32m 147\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m procs:\n\u001b[0;32m 148\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnbdev\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mexport\u001b[39;00m\n\u001b[1;32m--> 149\u001b[0m procs \u001b[38;5;241m=\u001b[39m [\u001b[38;5;28mgetattr\u001b[39m(\u001b[43mnbdev\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexport\u001b[49m, p) \u001b[38;5;28;01mfor\u001b[39;00m p \u001b[38;5;129;01min\u001b[39;00m L(procs)]\n\u001b[0;32m 150\u001b[0m files \u001b[38;5;241m=\u001b[39m nbglob(path\u001b[38;5;241m=\u001b[39mpath, as_path\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\u001b[38;5;241m.\u001b[39msorted(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mname\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 151\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m f \u001b[38;5;129;01min\u001b[39;00m files: nb_export(f, procs\u001b[38;5;241m=\u001b[39mprocs)\n",

nbstata/browse.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def set_ipydatagrid_height():
203203
display(HTML("<style>div.jp-Notebook .datagrid-container {min-height: 448px; }</style>"))
204204

205205
# %% ../nbs/07_browse.ipynb 53
206-
def display_df_as_ipydatagrid(df):
206+
def display_df_as_ipydatagrid(df, auto_height=True):
207207
from ipydatagrid import DataGrid, TextRenderer
208208
i_renderer = TextRenderer(horizontal_alignment="right", background_color="rgb(243, 243, 243)")
209209
d_renderer = TextRenderer(horizontal_alignment="right")
@@ -225,18 +225,20 @@ def name_width(name_len):
225225
20 + char_px_width*max((len(str(value)) for value in temp_head[name])),
226226
)
227227
column_widths[" "] = 20 + char_px_width*len(str(max(df.index.values)))
228-
g = DataGrid(df,
229-
index_name=" ",
230-
editable=False,
231-
selection_mode='cell',
232-
base_row_size=21,
233-
auto_fit_columns=False,
234-
default_renderer=d_renderer,
235-
header_renderer=h_renderer,
236-
renderers={" ": i_renderer},
237-
column_widths=column_widths,
238-
#layout={"height": "100%"},
239-
)
228+
g_kwargs = dict(
229+
index_name=" ",
230+
editable=False,
231+
selection_mode='cell',
232+
base_row_size=21,
233+
auto_fit_columns=False,
234+
default_renderer=d_renderer,
235+
header_renderer=h_renderer,
236+
renderers={" ": i_renderer},
237+
column_widths=column_widths,
238+
)
239+
if auto_height:
240+
g_kwargs['layout'] = {"height": "100%"}
241+
g = DataGrid(df, **g_kwargs)
240242
g.grid_style = {
241243
"background_color": "rgb(255, 255, 255)",
242244
"header_background_color": "rgb(243, 243, 243)",

settings.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ keywords = nbdev jupyter notebook python stata
2525
language = English
2626
status = 3
2727
user = hugetim
28-
requirements = jupyter-client ipython ipykernel packaging pandas numpy beautifulsoup4 fastcore pygments>=2.8 ipydatagrid
28+
requirements = jupyter-client ipython ipykernel packaging pandas numpy beautifulsoup4 fastcore pygments>=2.8 ipydatagrid<1.3
2929
black_formatting = False
3030
readme_nb = index.ipynb
3131
allowed_metadata_keys =

0 commit comments

Comments
 (0)