Skip to content

Commit

Permalink
Add usage
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmojaki committed Oct 19, 2023
1 parent 301d73a commit d527cdc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
The experimental JupyterLite Notebook widget lets you run custom Python code in [JupyterLite](https://jupyterlite.readthedocs.io/), a version of [JupyterLab](https://jupyterlab.readthedocs.io/en/stable/index.html) running entirely in the browser. You can use the full [custom widget plugin API](https://support.getgrist.com/code/modules/grist_plugin_api/) and access or modify any data in the document (subject to Access Rules), unlocking nearly unlimited possibilities for advanced users.

In the custom widget configuration, choose “Custom URL” and paste the following URL:

https://gristlabs.github.io/jupyterlite-widget/lab/index.html

You’ll be presented with a blank notebook where you can enter and run Python code, e.g:

![Blank notebook](./images/Screenshot%20from%202023-10-06%2014-38-15.png)

After typing code in a cell, click the play button or press Shift+Enter to run that cell.

Unlike formulas, code isn’t saved automatically. You must press the usual ‘Save’ button above the widget (outside the notebook) to persist the code within your Grist document. On the other hand, changes to settings within the notebook (e.g. keyboard shortcuts) are saved in your browser’s local storage, so they’re not shared with other users of the document.

A special object called `grist` is automatically available to use in Python code. In particular, `grist.raw` provides the usual [plugin API](https://support.getgrist.com/code/modules/grist_plugin_api/). All the methods are asynchronous, so you should use `await` before every call.

### Callbacks

You can decorate functions to make them run automatically when data is changed or the cursor moves in a selected widget. For example, run this code:

```
@grist.on_record
async def show_record(show, record, column_mappings):
show(record)
```

Then under the Data section in the right panel, click the “SELECT BY” dropdown and choose another widget. If there aren’t any options, add a widget to the page whose source data is the same table as that of the notebook widget. Now when you move the cursor between rows in the other widget, the notebook should automatically update and display the selected row.

Similarly, if you use `@grist.on_records` (note the extra `s` at the end) then the function will automatically run when the source data of the widget changes.

Note that the decorated function must have three arguments. The first argument (called `show` in the example above) should be called to display any kind of outputs instead of calling `print` or `display`. The third argument can be ignored.
Binary file added images/Screenshot from 2023-10-06 14-38-15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d527cdc

Please sign in to comment.