Skip to content

Commit

Permalink
Add workaround for Jupyter Lab 3 / ipywidget 7 compatibility
Browse files Browse the repository at this point in the history
Signed-off-by: gjmooney <gjmooney@asu.edu>
  • Loading branch information
gjmooney committed Mar 27, 2024
1 parent b0b5c00 commit 9293799
Show file tree
Hide file tree
Showing 9 changed files with 11,504 additions and 8,187 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
js/datagrid.ts
159 changes: 159 additions & 0 deletions examples/HideColumns.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "180ad1334fb24ddba2b58bdf07ce73ee",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"DataGrid(auto_fit_columns=True, auto_fit_params={'area': 'all', 'padding': 30, 'numCols': None}, corner_render…"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from ipydatagrid import DataGrid, TextRenderer\n",
"from json import load\n",
"import pandas as pd\n",
"\n",
"with open(\"./cars.json\") as fobj:\n",
" data = load(fobj)\n",
"\n",
"df = pd.DataFrame(data[\"data\"]).drop(\"index\", axis=1)\n",
"\n",
"datagrid = DataGrid(df, selection_mode=\"cell\")\n",
"datagrid.auto_fit_columns = True\n",
"datagrid"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "'Horsepower'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[2], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mrenderers\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mHorsepower\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[38;5;241m.\u001b[39mbackground_color \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myellow\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 2\u001b[0m datagrid\u001b[38;5;241m.\u001b[39mrenderers \u001b[38;5;241m=\u001b[39m renderers\n",
"\u001b[0;31mKeyError\u001b[0m: 'Horsepower'"
]
}
],
"source": [
"renderers[\"Horsepower\"].background_color = \"yellow\"\n",
"datagrid.renderers = renderers"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Cylinders': TextRenderer(background_color=Expr(value='default_value'), horizontal_alignment='right', text_color=Expr(value='default_value'))}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"renderers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"renderers = {\n",
" \"Origin\": TextRenderer(\n",
" horizontal_alignment = 'right'\n",
" ),\n",
"}\n",
"datagrid.renderers = renderers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"renderers = {\n",
" \"Origin\": TextRenderer(\n",
" horizontal_alignment = 'right'\n",
" ),\n",
" \"Acceleration\": TextRenderer(\n",
" horizontal_alignment = 'right'\n",
" ),\n",
" \"Cylinders\": TextRenderer(\n",
" horizontal_alignment = 'right'\n",
" ),\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"renderers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
2 changes: 2 additions & 0 deletions js/cellrenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ export class HtmlRendererModel extends CellRendererModel {

export class HtmlRendererView extends CellRendererView {
createRenderer(options: HtmlRenderer.IOptions) {
// Workaround for Jupyter Lab 3 / ipywidget 7 compatibility
let htmRenderer: any;

if (!AsyncCellRenderer) {
Expand All @@ -606,6 +607,7 @@ export class HtmlRendererView extends CellRendererView {
return htmRenderer;
}

// Workaround for Jupyter Lab 3 / ipywidget 7 compatibility
renderer: any;

model: HtmlRendererModel;
Expand Down
103 changes: 52 additions & 51 deletions js/core/htmlRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,7 @@ import {
import { ISignal, Signal } from '@lumino/signaling';
import { Theme } from '../utils';

/**
* The namespace for the `HtmlRenderer` class statics.
*/
export namespace HtmlRenderer {
/**
* An options object for initializing a html renderer.
*/
export interface IOptions extends TextRenderer.IOptions {
/**
* The font for drawing the cell text.
*
* The default is `'12px sans-serif'`.
*/
font?: CellRenderer.ConfigOption<string>;

/**
* The string to be displayed when placeholder is painted
*
* The default is `'...'`.
*/
placeholder?: CellRenderer.ConfigOption<string>;

/**
* The color for the drawing the cell text.
*
* The default `'#000000'`.
*/
textColor?: CellRenderer.ConfigOption<string>;

/**
* The vertical alignment for the cell text.
*
* The default is `'center'`.
*/
verticalAlignment?: CellRenderer.ConfigOption<TextRenderer.VerticalAlignment>;
/**
* The horizontal alignment for the cell text.
*
* The default is `'left'`.
*/
horizontalAlignment?: CellRenderer.ConfigOption<TextRenderer.HorizontalAlignment>;
/**
* The format function for the renderer.
*
* The default is `TextRenderer.formatGeneric()`.
*/
// format?: FormatFunc;
}
}

// Workaround for Jupyter Lab 3 / ipywidget 7 compatibility
let exportedClass: any;

/**
Expand Down Expand Up @@ -289,4 +239,55 @@ if (AsyncCellRenderer) {
};
}

/**
* The namespace for the `HtmlRenderer` class statics.
*/
export namespace HtmlRenderer {
/**
* An options object for initializing a html renderer.
*/
export interface IOptions extends TextRenderer.IOptions {
/**
* The font for drawing the cell text.
*
* The default is `'12px sans-serif'`.
*/
font?: CellRenderer.ConfigOption<string>;

/**
* The string to be displayed when placeholder is painted
*
* The default is `'...'`.
*/
placeholder?: CellRenderer.ConfigOption<string>;

/**
* The color for the drawing the cell text.
*
* The default `'#000000'`.
*/
textColor?: CellRenderer.ConfigOption<string>;

/**
* The vertical alignment for the cell text.
*
* The default is `'center'`.
*/
verticalAlignment?: CellRenderer.ConfigOption<TextRenderer.VerticalAlignment>;
/**
* The horizontal alignment for the cell text.
*
* The default is `'left'`.
*/
horizontalAlignment?: CellRenderer.ConfigOption<TextRenderer.HorizontalAlignment>;
/**
* The format function for the renderer.
*
* The default is `TextRenderer.formatGeneric()`.
*/
// format?: FormatFunc;
}
}

export { exportedClass };
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"@babel/core": "^7.6.3",
"@babel/preset-env": "^7.6.3",
"@babel/preset-typescript": "^7.6.0",
"@jupyterlab/builder": "^3.6.7",
"@jupyterlab/builder": "^4.0.5",
"@types/jest": "^27.4.1",
"@types/node": "^10.11.6",
"@types/underscore": "^1.11.4",
Expand Down Expand Up @@ -114,7 +114,7 @@
"svg-url-loader": "~3.0.3",
"ts-jest": "^28.0.8",
"ts-loader": "^6.0.4",
"typescript": "~4.5.0",
"typescript": "~4.2.4",
"url-loader": "^4.1.0",
"webpack": "^5.88.2",
"webpack-cli": "^4.4.0"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
build-backend = "hatchling.build"
requires = [
"hatchling>=1.18",
"jupyterlab==3.6.7",
"jupyterlab>=4.0.5",
]

[project]
Expand Down
1 change: 1 addition & 0 deletions style/icons/search-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions style/icons/search.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 9293799

Please sign in to comment.