documentsearch
command in a notebook code cell via ipylab¶Demo of searching for a specific word in a notebook document programmatically.
This is in regards to the Jupyter Discourse thread 'Targeted DocumentSearch command', expanding on my reply here with the proper syntax provided by Michał Krassowski here.
This notebook was developed in a session launched from Jeremy Toloup's page to offer 'JupyterLab 4.3 on Binder', and then adapted to work with sessions launched from here via nbgitpuller.
Important Note: You'll need to get ipylab working fully first. See the bottom of my answer here because that probabaly will require a hard refresh of the browser page after installing it.
%pip install ipylab -q
Note: you may need to restart the kernel to use updated packages.
from ipylab import JupyterFrontEnd
from ipywidgets import Output
app = JupyterFrontEnd()
out = Output()
def init():
# show a slice of the available commands
cmds = app.commands.list_commands()[53:63]
out.append_stdout(cmds)
app.on_ready(init)
out
Output()
Now if running that code above displays a list of a few commands, then we are ready to try search.
(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.)
The first two cells just below this will set things up with text we can look for.
Here is a needle in a tiny haystack of markdown. Can we find it?
a_var = "Here is a needle in a string of code. Can we find that?"
# Now try to search for the string 'needle' using code in a cell to search for a specific word programmatically!
from ipylab import JupyterFrontEnd
from ipywidgets import Output
app = JupyterFrontEnd()
out = Output()
def init():
# show a slice of the available commands
cmds = app.commands.execute('documentsearch:start', {"searchText": "needle"})
out.append_stdout(cmds)
app.on_ready(init)
out
Output()
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.