Search in Jupyter Notebooks

Testing out the techniques in the Hypermodern Python article series.

Join the Code Quality for Data Science (CQ4ds) Discord channel

The command-line interface prints a pandas dataframe to the console.

Installation

To install the package, run this command in your terminal:

$ pip install nb-query

Usage

Notebook Query’s usage looks like:

$ nb-query [QUERY]
--help

Display a short usage message and exit.

Usage in notebooks

from nb_query import nb_query
nb_query('Hello')
nb_query('Hello', 'data/notebooks')
nb_query('He(ll|r)o')
nb_query(lambda line: len(line) == 2)

License

MIT License

Copyright (c) 2022 Laszlo Sragner

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Reference

nb_query.main

Function to search in selected notebooks with keywords, regex or functions.

main.main(query, fnames=None)

Command line wrapper for Typer. Searches all files in the given directories.

Parameters
  • query (str) – Keyword, regex expression or function

  • fnames (Optional[str], optional) – directory to search. Defaults to current directory.

Return type

None

main.nb_query(query, fnames=None)

Function to search in selected notebooks with keywords, regex or functions.

Parameters
  • query (str) – Keyword, regex expression or function

  • fnames (Union[None, str, List[str]]) – str: directory to search (recursively). None: current directory List[str]: list of notebooks to search

Returns

Table of results: notebook location, line matching the query, cell number and cell count

Return type

pd.DataFrame