Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to search over jupyter notebook python files?

I have many jupyter python files (ipnyb), how can I easily search over python source code over them, for example from command line or jupyter-notebook client itself? I am looking for something dedicated to jupter notebooks instead of normal find/grep because embedded data " "data": { "image/png" }

like image 369
karol Avatar asked Mar 28 '20 20:03

karol


Video Answer


3 Answers

To search inside a notebook, use the find+replace shortcut, F, in command mode. For example, on Kaggle, Esc for command mode, then F to trigger find.

Or open command palette with Cmd+Shift+P on Mac, Ctrl+Shift+P on Linux/Win. Then just type "find" in it.

As for searching over multiple notebooks at once within Jupyter, I have no experience. I suggest looking into JupyterLabs and its extensions, since it seems more geared for multiple notebooks.

From CLI, plain grep works

grep wordtosearch *.ipynb

For recursion

grep -r --include \*.ipynb wordtosearch mytopdirectory
like image 98
kg_sYy Avatar answered Dec 06 '22 23:12

kg_sYy


This command allow you to perform a recursive search (through all directories from the current directory)

For explanations about this command check this link => explainshell

find . -name '*ipynb' | xargs grep YOUR_SEARCH
like image 20
Mathix420 Avatar answered Dec 07 '22 00:12

Mathix420


I was looking to do this, too! I found a pretty good solution that you can do from within a notebook. The google groups discussion is here, but in case the link breaks, here is the code provided there:


pattern = '../**/*.ipynb'
query = 'text you want to find'

for filepath in glob.iglob(pattern, recursive=True):
    with open(filepath) as file:
        s = file.read()
        if (s.find(query) > -1):
            print(filepath)

This will print all of the files that contain the search string. A nice enhancement would be to also print the context in which the string occurs.

like image 41
Tom F Avatar answered Dec 06 '22 22:12

Tom F