I found the command documentsearch:start that basically is the same as Ctrl+F, I guess this command is meant to be used in code. Is there a way to pass an argument to it (or some other command) to search for a specific word programmatically ?
Yes! It accepts searchText
argument, we use it programatically in UI tests here:
Thank you! Another question, is this strictly on the Java side or is there a way to use them directly from the python cells ?
In theory, ipylab exposes it for use:
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
From that code you’ll see currently:
apputils:run-all-enabled
apputils:display-shortcuts
documentsearch:start
documentsearch:startWithReplace
documentsearch:highlightNext
documentsearch:highlightPrevious
documentsearch:end
documentsearch:toggleSearchInSelection
help:about
help:jupyter-forum
I couldn’t quite come up with the proper syntax for a use case example though.
Use of app.on_ready(init)
and a hard browser page refrsh to get app.commands.list_commands()
to work is discussed in my comment here, which is a comment below a post by Michał Krassowski that has a nice succinct example of ipylab use via python.
That combination of steps should allow the code I provide above to work in MyBinder launches from here at present. After the session spins up and you open a new notebook, then run %pip install ipylab
. After that completes, restart the kernel and do a hard refresh. Only after all those steps, should you try the code above.
This should work:
app.commands.execute('documentsearch:start', {"searchText": "needle"})
Try it yourself
I used that syntax to put together a demonstration notebook that one can easily run right in the browser without downloading or installing anything.
Click here to launch a session served by MyBinder that will step through the process.
The key is getting ipylab
to work in the session first, and then the text search can be done programmatically in a code.
The code boils down to:
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
(The static version of the full demonstration notebook can be seen here; however, launching a session where it is active and working is far better.)