Skip to main content

Add watchdog to IPython cells

Project description

watchmagic adds %%watch magic to IPython. When added to a cell, it watches a given directory for file changes. Once a file is changed, the cell is re-evaluated.

watchmagic’s real power lies in combining it with %autoreload which allows to edit code in an external editor while watching the changes in a notebook in real-time on every save.

License

Copyright 2020 Gunnar Voet

watchmagic is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

watchmagic is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with watchmagic. If not, see <http://www.gnu.org/licenses/>.

Installation

Install via pip:

pip install watchmagic

Usage

In the notebook run

%load_ext watchmagic

Now you can use the magic by simply putting %%watch on the first line of the cell you are working on. This will watch the current directory and its subdirectories for file changes. For example:

%%watch
print('hello world')

will watch the current directory and run the print statement on any file changes. Interrupt the kernel to quit watching for file changes.

To make the magic available by default, add watchmagic to your ipython_config.py:

c.InteractiveShellApp.extensions = ['watchmagic']

Note that this is not the jupyter config file, but it will work in jupyter notebooks.

The real power of the %%watch magic lies in combining it with IPython’s %autoreload magic. It allows to develop code in an external editor while watching the results in real time on every file save. For example, if working on the new function overview_plot() in the package science_plots, runnig the following in a jupyter notebook will provide a real time view of the results:

import science_plots
%load_ext autoreload
%autoreload 2  # reload all packages
%load_ext watchmagic
%%watch --ignore *.ipynb
science_plots.overview_plot()

Note that here we are watching the current directory by default, so science_plots must be either in the local directory or one of its subdirectories. %%watch will also ignore changes in jupyter notebook files such that the overview_plot() is not run when the notebook is saved (and no changes have ocurred in the source code).

Options

Several command line options exist. Run %%watch? to display the following help screen:

%watch [-p PATH] [-r] [-nr]
            [--patterns [PATTERNS [PATTERNS ...]]]
            [--ignore [IGNORE_PATTERNS [IGNORE_PATTERNS ...]]]

Watch for file changes and re-evaluate current cell.

optional arguments:
-p PATH, --path PATH  provides the PATH to watch for file changes. If
                        not provided, the current directory will be
                        watched.
-r, --recursive       include subdirectories recursively when
                        watching for file changes. This is the default
                        behaviour.
-nr, --non-recursive  do not include subdirectories when watching for
                        file changes.
--patterns <[PATTERNS [PATTERNS ...]]>
                        look for files with these PATTERNS.
--ignore <[IGNORE_PATTERNS [IGNORE_PATTERNS ...]]>
                        ignore files containing IGNORE_PATTERNS when
                        watching for file changes.

Config

The following default settings can be changed in ipython_config.py:

#---------------------------------------------------------------
# WatchMagics configuration
#---------------------------------------------------------------
c.WatchMagics.default_patterns = ['*.py']
c.WatchMagics.default_ignore_patterns = ['*.ipynb']
c.WatchMagics.default_case_sensitive = False
c.WatchMagics.default_ignore_directories = True

Command line options (if provided) will override the default settings.

History

0.1.1 (2020-06-20)

  • Fix bug in dist build.

0.1.0 (2020-06-18)

  • Initial release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

watchmagic-0.1.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

watchmagic-0.1.1-py2.py3-none-any.whl (18.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file watchmagic-0.1.1.tar.gz.

File metadata

  • Download URL: watchmagic-0.1.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1.post20200616 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.6

File hashes

Hashes for watchmagic-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8b728ce2cabc6d0c3ece10d1d7073723e1e68a38d703881fe0b96b6fdbbc9a15
MD5 e3716e36bc79c2658b6888e5164ab721
BLAKE2b-256 b5a2def66eb2145f1bc81d4902a09531dbd11f6a7ca65a21a727a4fc41fde8f5

See more details on using hashes here.

File details

Details for the file watchmagic-0.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: watchmagic-0.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1.post20200616 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.6

File hashes

Hashes for watchmagic-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 91e62ff417584d4c9bda32c3dd94cd6c8aa2283da5662d9bda3a344441d3e196
MD5 5afb61c17dc6403ea4f3c0a4d429cfc4
BLAKE2b-256 ed30242cfc6b20b1e9bea69850a9e8972ceaea1d9449abbdfd5e9a94a64b8162

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page