mwouts / itables

Pandas DataFrames as Interactive DataTables
https://mwouts.github.io/itables/
MIT License
740 stars 55 forks source link

Notebook extension to enable itables interactive mode #54

Closed djangoliv closed 2 years ago

djangoliv commented 2 years ago

it might be nice to have a button to enable interactive mode.

A simple example of code for that:

import { DocumentRegistry } from "@jupyterlab/docregistry";
import { INotebookModel, NotebookPanel } from "@jupyterlab/notebook";
import { DisposableDelegate, IDisposable } from "@lumino/disposable";
import { ToolbarButton } from "@jupyterlab/apputils";

export class ItablesButton
  implements DocumentRegistry.IWidgetExtension<NotebookPanel, INotebookModel>
{
  public createNew(panel: NotebookPanel): IDisposable {
    const callback = () => {
      this.itables(panel);
    };
    const button = new ToolbarButton({
      className: "itablesButton",
      label: "Itables",
      onClick: callback,
      tooltip: "Activate the itables interactive mode",
    });

    panel.toolbar.insertItem(0, "itables", button);
    return new DisposableDelegate(() => {
      button.dispose();
    });
  }

  // launch itables
  protected itables = (panel: NotebookPanel) => {
    const kernelConnection = panel.context.sessionContext.session.kernel;
    kernelConnection.requestExecute({
      code: "from itables import init_notebook_mode; init_notebook_mode(all_interactive=True)",
    });
  };
}

Regards

mwouts commented 2 years ago

Hi @djangoliv , thank you for the suggestion and for the code snippet. Yes indeed I do like the idea to have something like an on/off toggle, that sounds great.

On the way towards such an extension I see two points that we should discuss:

Let me know if you have any advice regarding the above. Thanks!

djangoliv commented 2 years ago

Hello @mwouts

You're right, it may be better to make two separate repository (a jupyterlab-itables project).

Indeed, the output is not displayed and the init_notebook_mode code will not be present in the HTML export.

Personally it doesn't bother me. Since the code just replaces the table visualization. The exported code will remain consistent and display the tables in a classic way.

Otherwise, the button could generate and execute the init cell at the beginning of the notebook.

mwouts commented 2 years ago

Hello @djangoliv , I have created a draft extension at https://github.com/mwouts/jupyterlab-itables

However when I try to install it with

jupyter labextension develop . --overwrite

then I run into this error:

    src/index.ts(7,63): error TS2344: Type 'INotebookModel' does not satisfy the constraint 'IModel'.
      Types of property 'sharedModel' are incompatible.
        Property 'dirty' is missing in type 'ISharedNotebook' but required in type 'ISharedDocument'.

Do you think you can iterate on top of my draft? Thanks!

(full logs) /home/marc/GitHub/jupyterlab-itables/setup.py:65: DeprecatedWarning: create_cmdclass is deprecated as of 0.8 and will be removed in 1.0. " Use `wrap_installers` to handle prebuild steps in cmdclass. Use `get_data_files` to handle data files. Use `include_package_data=True` and `MANIFEST.in` for package data. cmdclass = create_cmdclass( /home/marc/GitHub/jupyterlab-itables/setup.py:71: DeprecatedWarning: install_npm is deprecated as of 0.8 and will be removed in 1.0. Use `npm_builder` and `wrap_installers` install_npm( Obtaining file:///home/marc/GitHub/jupyterlab-itables Installing collected packages: jupyterlab-itables Running setup.py develop for jupyterlab-itables ERROR: Command errored out with exit status 1: command: /home/marc/miniconda3/envs/jupytext-dev/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/home/marc/GitHub/jupyterlab-itables/setup.py'"'"'; __file__='"'"'/home/marc/GitHub/jupyterlab-itables/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' develop --no-deps cwd: /home/marc/GitHub/jupyterlab-itables/ Complete output (73 lines): /home/marc/GitHub/jupyterlab-itables/setup.py:65: DeprecatedWarning: create_cmdclass is deprecated as of 0.8 and will be removed in 1.0. " Use `wrap_installers` to handle prebuild steps in cmdclass. Use `get_data_files` to handle data files. Use `include_package_data=True` and `MANIFEST.in` for package data. cmdclass = create_cmdclass( /home/marc/GitHub/jupyterlab-itables/setup.py:71: DeprecatedWarning: install_npm is deprecated as of 0.8 and will be removed in 1.0. Use `npm_builder` and `wrap_installers` install_npm( running develop running jsdeps yarn not found, ignoring yarn.lock file Installing build dependencies with npm. This may take a while... > jlpm install yarn install v1.22.17 [1/4] Resolving packages... [2/4] Fetching packages... [3/4] Linking dependencies... warning "@jupyterlab/application > @jupyterlab/ui-components@3.2.4" has unmet peer dependency "react@^17.0.1". warning "@jupyterlab/application > @lumino/coreutils@1.11.1" has unmet peer dependency "crypto@1.0.1". warning "@jupyterlab/docregistry > @jupyterlab/ui-components@3.2.8" has unmet peer dependency "react@^17.0.1". warning "@jupyterlab/builder > @jupyterlab/buildutils > verdaccio > clipanion@3.1.0" has unmet peer dependency "typanion@*". warning Workspaces can only be enabled in private projects. warning Workspaces can only be enabled in private projects. [4/4] Building fresh packages... success Saved lockfile. $ jlpm run clean && jlpm run build:prod yarn run v1.22.17 $ jlpm run clean:lib $ rimraf lib tsconfig.tsbuildinfo Done in 1.61s. yarn run v1.22.17 $ jlpm run build:lib && jlpm run build:labextension $ tsc src/index.ts(7,63): error TS2344: Type 'INotebookModel' does not satisfy the constraint 'IModel'. Types of property 'sharedModel' are incompatible. Property 'dirty' is missing in type 'ISharedNotebook' but required in type 'ISharedDocument'. error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command. Traceback (most recent call last): File "", line 1, in File "/home/marc/GitHub/jupyterlab-itables/setup.py", line 81, in setup(**setup_args) File "/home/marc/miniconda3/envs/jupytext-dev/lib/python3.9/site-packages/setuptools/__init__.py", line 153, in setup return distutils.core.setup(**attrs) File "/home/marc/miniconda3/envs/jupytext-dev/lib/python3.9/distutils/core.py", line 148, in setup dist.run_commands() File "/home/marc/miniconda3/envs/jupytext-dev/lib/python3.9/distutils/dist.py", line 966, in run_commands self.run_command(cmd) File "/home/marc/miniconda3/envs/jupytext-dev/lib/python3.9/distutils/dist.py", line 985, in run_command cmd_obj.run() File "/home/marc/miniconda3/envs/jupytext-dev/lib/python3.9/site-packages/jupyter_packaging/setupbase.py", line 637, in run [self.run_command(cmd) for cmd in cmds] File "/home/marc/miniconda3/envs/jupytext-dev/lib/python3.9/site-packages/jupyter_packaging/setupbase.py", line 637, in [self.run_command(cmd) for cmd in cmds] File "/home/marc/miniconda3/envs/jupytext-dev/lib/python3.9/distutils/cmd.py", line 313, in run_command self.distribution.run_command(command) File "/home/marc/miniconda3/envs/jupytext-dev/lib/python3.9/distutils/dist.py", line 985, in run_command cmd_obj.run() File "/home/marc/miniconda3/envs/jupytext-dev/lib/python3.9/site-packages/jupyter_packaging/setupbase.py", line 318, in run c.run() File "/home/marc/miniconda3/envs/jupytext-dev/lib/python3.9/site-packages/jupyter_packaging/setupbase.py", line 612, in run builder() File "/home/marc/miniconda3/envs/jupytext-dev/lib/python3.9/site-packages/jupyter_packaging/setupbase.py", line 211, in builder run(npm_cmd + ['install'], cwd=node_package) File "/home/marc/miniconda3/envs/jupytext-dev/lib/python3.9/site-packages/jupyter_packaging/setupbase.py", line 272, in run return subprocess.check_call(cmd, **kwargs) File "/home/marc/miniconda3/envs/jupytext-dev/lib/python3.9/subprocess.py", line 373, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '['/home/marc/miniconda3/envs/jupytext-dev/bin/jlpm', 'install']' returned non-zero exit status 1. ---------------------------------------- ERROR: Command errored out with exit status 1: /home/marc/miniconda3/envs/jupytext-dev/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/home/marc/GitHub/jupyterlab-itables/setup.py'"'"'; __file__='"'"'/home/marc/GitHub/jupyterlab-itables/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' develop --no-deps Check the logs for full command output. An error occurred. subprocess.CalledProcessError: Command '['/home/marc/miniconda3/envs/jupytext-dev/bin/python', '-m', 'pip', 'install', '-e', '/home/marc/GitHub/jupyterlab-itables']' returned non-zero exit status 1. See the log file for details: /tmp/jupyterlab-debug-68nqiy_h.log
djangoliv commented 2 years ago

@mwouts See: https://github.com/mwouts/jupyterlab-itables/pull/1

mwouts commented 2 years ago

Hi @djangoliv , I am going to close this issue because the init_notebook_mode function changed significantly in itables==1.0.0.

In particular it now has outputs, and we need these outputs in the notebook, so I am afraid the solution that you considered here would only work for init_notebook_mode(connected=True) while the default is now init_notebook_mode(connected=False).

Anyway let me thank you for suggesting this (and use connected=False in your local extension if you want to continue using it!)