Skip to main content

watchmagic

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.

Metadata

Release files for watchmagic 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for watchmagic 0.1.1
File Size Uploaded
watchmagic-0.1.1.tar.gz 6.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for watchmagic 0.1.1
File Interpreter ABI Platform
watchmagic-0.1.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 24.9 kB

Release files / watchmagic-0.1.1.tar.gz

Download URL watchmagic-0.1.1.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
8b728ce2cabc6d0c3ece10d1d7073723e1e68a38d703881fe0b96b6fdbbc9a15
BLAKE2b-256 checksum
How to use checksums
b5a2def66eb2145f1bc81d4902a09531dbd11f6a7ca65a21a727a4fc41fde8f5
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / watchmagic-0.1.1-py2.py3-none-any.whl

Download URL watchmagic-0.1.1-py2.py3-none-any.whl
Size 18.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
91e62ff417584d4c9bda32c3dd94cd6c8aa2283da5662d9bda3a344441d3e196
BLAKE2b-256 checksum
How to use checksums
ed30242cfc6b20b1e9bea69850a9e8972ceaea1d9449abbdfd5e9a94a64b8162
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page