Created
May 16, 2025 16:20
-
-
Save fomightez/9bccb255d9ac4e9deb37a4f63f6a440c to your computer and use it in GitHub Desktop.
Demonstrate using documentsearch in a Jupyter Notebook via ipylab for programmatic text search, in reply to a Jupyter Discourse thread https://discourse.jupyter.org/t/targeted-documentsearch-command/34957?u=fomightez
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "bfbdc757-0e71-4fad-9ff3-1a9e6f693e19", | |
"metadata": {}, | |
"source": [ | |
"# Demonstrate using `documentsearch` command in a notebook code cell via ipylab\n", | |
"\n", | |
"Demo of searching for a specific word in a notebook document programmatically.\n", | |
"\n", | |
"This is in regards to [the Jupyter Discourse thread 'Targeted DocumentSearch command'](https://discourse.jupyter.org/t/targeted-documentsearch-command/34957?u=fomightez), expanding on my reply [here](https://discourse.jupyter.org/t/targeted-documentsearch-command/34957/4?u=fomightez) with the proper syntax provided by [Michał Krassowski here](https://discourse.jupyter.org/t/targeted-documentsearch-command/34957/5?u=fomightez).\n", | |
"\n", | |
"This notebook was developed in a session launched from [Jeremy Toloup's page to offer 'JupyterLab 4.3 on Binder'](https://gist.github.com/jtpio/f3f7aed7c87bb15e07009bb29a601ed8), and then adapted to work [with sessions launched from here via nbgitpuller](https://gist.github.com/fomightez/6773dedf6d5132795dd4245a18c066eb). \n", | |
"\n", | |
"--- \n", | |
"\n", | |
"**Important Note:** You'll need to get ipylab working fully first. See [the bottom of my answer here](https://discourse.jupyter.org/t/targeted-documentsearch-command/34957/4?u=fomightez) because that probabaly will require a hard refresh of the browser page after installing it." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "83c3b566-0e1c-41ea-8851-39186f1b58eb", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Note: you may need to restart the kernel to use updated packages.\n" | |
] | |
} | |
], | |
"source": [ | |
"%pip install ipylab -q" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "0e4ab839-fae0-4262-a17a-4beac3ec65df", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "e93d530a260b40ceae24f87cd8f85636", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"Output()" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"from ipylab import JupyterFrontEnd\n", | |
"from ipywidgets import Output\n", | |
"\n", | |
"app = JupyterFrontEnd()\n", | |
"out = Output()\n", | |
"\n", | |
"def init():\n", | |
" # show a slice of the available commands\n", | |
" cmds = app.commands.list_commands()[53:63]\n", | |
" out.append_stdout(cmds)\n", | |
"\n", | |
"app.on_ready(init)\n", | |
"out" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "61255e4f-9b41-4012-89dc-e7fe5166c444", | |
"metadata": {}, | |
"source": [ | |
"**Now if running that code above displays a list of a few commands, then we are ready to try search.** \n", | |
"(If it didn't show anything in the output, yet you see no error, you probably need a hard refresh. Try to do a hard refresh on this page and then re-run the cell again until it lists a few commands. See the header section of this notebook above for a 'Note' with more about this.)\n", | |
"\n", | |
"The first two cells just below this will set things up with text we can look for." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "ae7ece75-08f3-4b44-af5d-540d9cce5c4a", | |
"metadata": {}, | |
"source": [ | |
"Here is a needle in a tiny haystack of markdown. Can we find it?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "cf34d7c6-cc8e-4e86-aadd-d2631c64f582", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"a_var = \"Here is a needle in a string of code. Can we find that?\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "71461190-1857-4b50-a0a2-8f764b8ce7b9", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "6c430b30cf9f46beadf54ba35c97fdbf", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"Output()" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Now try to search for the string 'needle' using code in a cell to search for a specific word programmatically!\n", | |
"from ipylab import JupyterFrontEnd\n", | |
"from ipywidgets import Output\n", | |
"\n", | |
"app = JupyterFrontEnd()\n", | |
"out = Output()\n", | |
"\n", | |
"def init():\n", | |
" # show a slice of the available commands\n", | |
" cmds = app.commands.execute('documentsearch:start', {\"searchText\": \"needle\"})\n", | |
" out.append_stdout(cmds)\n", | |
"\n", | |
"app.on_ready(init)\n", | |
"out" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "26e18bc0-1575-452e-a2da-ad3b8723c67c", | |
"metadata": {}, | |
"source": [ | |
"After running that code, the JupyterLab find prompt should open and you should see 'needle' highlighted throughout the notebook document if you scroll up! It will be highlighted in both markdown cells and code cells." | |
] | |
} | |
], | |
"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.10.14" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment