Skip to main content

Simple, modern and high performance file watching and code reload in python.

Project description

watchfiles

CI Coverage pypi license

Simple, modern and high performance file watching and code reload in python.


NOTICE

This package was significantly altered and renamed from watchgod to watchfiles, this files refers to the watchfiles package.

Documentation for the old version (watchgod) is available here. See issue #102 for details on the migration and its rationale.


Underlying file system notifications are handled by the Notify rust library.

Installation

watchfiles requires Python 3.7 - 3.10.

pip install watchfiles

Binaries are available for:

  • Linux: manylinux-x86_64, musllinux-x86_64 & manylinux-i686
  • MacOS: x86_64 & arm64 (except python 3.7)
  • Windows: amd64 & win32

Otherwise, you can install from source which requires Rust stable to be installed.

Usage

To watch for changes in a directory:

from watchfiles import watch

for changes in watch('./path/to/dir'):
    print(changes)

watch (and all other methods described below) can take multiple paths as arguments to watch.

To run a function and restart it when code changes:

from watchfiles import run_process

def foobar(a, b, c):
    ...

if __name__ == '__main__':
    run_process('./path/to/dir', target=foobar, args=(1, 2, 3))

run_process uses PythonFilter by default so only changes to python files will prompt a reload, see custom event filtering below.

If you need notifications about change events as well as to restart a process you can use the callback argument to pass a function which will be called on every file change with one argument: the set of file changes.

File changes are also available via the WATCHFILES_CHANGES environment variable which contains JSON encoded details of changes, see the CLI example below.

Asynchronous Methods

watchfiles comes with an asynchronous equivalents of watch: awatch.

import asyncio
from watchfiles import awatch

async def main():
    async for changes in awatch('/path/to/dir'):
        print(changes)

asyncio.run(main())

There's also an asynchronous equivalents of run_process: arun_process which in turn uses awatch:

import asyncio
from watchfiles import arun_process

def foobar(a, b, c):
    ...

async def main():
    await arun_process('./path/to/dir', target=foobar, args=(1, 2, 3))

if __name__ == '__main__':
    asyncio.run(main())

The signature of arun_process is almost identical to run_process except that the optional callback argument may be a coroutine.

Custom Filters

The watch_filter argument to the above methods allows you to specify which file system events watchfiles should react to (either yield or reload code). watch_filter should just be a callable which takes a change (either "added", "modified" or "deleted") and a path (as a string) and should return whether or not that change should be registered.

watchfiles comes with the following classes, instances of which can be with watch_filter:

  • DefaultFilter The watcher used by default by watch and awatch, commonly ignored files like *.swp, *.pyc and *~ are ignored along with directories like .git.
  • PythonFilter Specific to python files, only *.py, *.pyx and *.pyd files are watched.
  • BaseFilter, used by DefaultFilter and PythonFilter, useful for defining your own filters which leverage the same logic

Here's an example of a custom filter which extends DefaultFilter to only notice changes to common web files:

from watchfiles import Change, DefaultFilter, watch


class WebFilter(DefaultFilter):
    allowed_extensions = '.html', '.css', '.js'

    def __call__(self, change: Change, path: str) -> bool:
        return super().__call__(change, path) and path.endswith(self.allowed_extensions)

for changes in watch('my/web/project', watch_filter=WebFilter()):
    print (changes)

Here's an example of a customer filter which is a simple callable that ignores changes unless they represent a new file being created:

from watchfiles import Change, watch

def only_added(change: Change, path: str) -> bool:
    return change == Change.added

for changes in watch('my/project', watch_filter=only_added):
    print (changes)

For more details, checkout filters.py, it's pretty simple.

CLI

watchfiles also comes with a CLI for running and reloading python code.

Let's say you have foobar.py (this is a very simple web server using aiohttp) which gets details about recent file changes from the WATCHFILES_CHANGES environment variable and returns them as JSON.

import os, json
from aiohttp import web

async def handle(request):
    # get the most recent file changes and return them
    changes = os.getenv('WATCHFILES_CHANGES', '[]')
    changes = json.loads(changes)
    return web.json_response(dict(changes=changes))

app = web.Application()
app.router.add_get('/', handle)

def main():
    web.run_app(app, port=8000)

You could run this and reload it when any file in the current directory changes with:

watchfiles foobar.main

Run watchfiles --help for more options.

The CLI can also be used via python -m watchfiles ....

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

watchfiles-0.12.tar.gz (19.8 kB view details)

Uploaded Source

Built Distributions

watchfiles-0.12-cp310-cp310-win_amd64.whl (204.7 kB view details)

Uploaded CPython 3.10 Windows x86-64

watchfiles-0.12-cp310-cp310-win32.whl (198.7 kB view details)

Uploaded CPython 3.10 Windows x86

watchfiles-0.12-cp310-cp310-manylinux_2_24_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.24+ x86-64

watchfiles-0.12-cp310-cp310-manylinux_2_24_i686.whl (1.1 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.24+ i686

watchfiles-0.12-cp310-cp310-macosx_11_0_arm64.whl (275.4 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

watchfiles-0.12-cp310-cp310-macosx_10_9_x86_64.whl (286.6 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

watchfiles-0.12-cp39-cp39-win_amd64.whl (204.9 kB view details)

Uploaded CPython 3.9 Windows x86-64

watchfiles-0.12-cp39-cp39-win32.whl (198.8 kB view details)

Uploaded CPython 3.9 Windows x86

watchfiles-0.12-cp39-cp39-manylinux_2_24_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.24+ x86-64

watchfiles-0.12-cp39-cp39-manylinux_2_24_i686.whl (1.1 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.24+ i686

watchfiles-0.12-cp39-cp39-macosx_11_0_arm64.whl (275.6 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

watchfiles-0.12-cp39-cp39-macosx_10_9_x86_64.whl (286.7 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

watchfiles-0.12-cp38-cp38-win_amd64.whl (204.4 kB view details)

Uploaded CPython 3.8 Windows x86-64

watchfiles-0.12-cp38-cp38-win32.whl (198.6 kB view details)

Uploaded CPython 3.8 Windows x86

watchfiles-0.12-cp38-cp38-manylinux_2_24_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.24+ x86-64

watchfiles-0.12-cp38-cp38-manylinux_2_24_i686.whl (1.1 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.24+ i686

watchfiles-0.12-cp38-cp38-macosx_11_0_arm64.whl (275.5 kB view details)

Uploaded CPython 3.8 macOS 11.0+ ARM64

watchfiles-0.12-cp38-cp38-macosx_10_9_x86_64.whl (287.1 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

watchfiles-0.12-cp37-cp37m-win_amd64.whl (204.3 kB view details)

Uploaded CPython 3.7m Windows x86-64

watchfiles-0.12-cp37-cp37m-win32.whl (198.5 kB view details)

Uploaded CPython 3.7m Windows x86

watchfiles-0.12-cp37-cp37m-manylinux_2_24_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.24+ x86-64

watchfiles-0.12-cp37-cp37m-manylinux_2_24_i686.whl (1.1 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.24+ i686

watchfiles-0.12-cp37-cp37m-macosx_10_9_x86_64.whl (287.1 kB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

File details

Details for the file watchfiles-0.12.tar.gz.

File metadata

  • Download URL: watchfiles-0.12.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.2

File hashes

Hashes for watchfiles-0.12.tar.gz
Algorithm Hash digest
SHA256 962aa5fcc212fe4d838f9b28fcf70b6a5c850930fc4d389f444edb821d264110
MD5 be646e57a2b14ae56e9cdef6fc2065bd
BLAKE2b-256 5fc74000aac83d364149a5092e4d4b48e2624d97206143a717ffb5949141e8df

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for watchfiles-0.12-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4d8a0aec717a14e6107babcf0d53b19cf341ca4947679783fc76e7afb154b7bc
MD5 2a38470a13d8d2f92371526f651f1a89
BLAKE2b-256 3a46fdd395a9be28dea92011d42ced073992fae040ad215a4f1cea65020c4bd7

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp310-cp310-win32.whl.

File metadata

  • Download URL: watchfiles-0.12-cp310-cp310-win32.whl
  • Upload date:
  • Size: 198.7 kB
  • Tags: CPython 3.10, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.2

File hashes

Hashes for watchfiles-0.12-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 717f8202e358d751fc811c6ccba5a8d19e16d84c3f91448d82dcfe4ba14349fd
MD5 d4712761598a53d7e05f262627663c28
BLAKE2b-256 f0b3caebbe184699d40d2a2dc127261e8fd9a70648be40031bd905ec0984bd6c

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp310-cp310-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for watchfiles-0.12-cp310-cp310-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 e2d9c5ee04d3da4c3393dea7de739646a7da67ca87fe5f6eacf20fa47aef9221
MD5 c31f07d49cce8576b18fb6cb867af1e0
BLAKE2b-256 c3e3a0b2577c7fdb45d4878ef8f008d7739ef72aecd928d063a02de5fd4fa515

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp310-cp310-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for watchfiles-0.12-cp310-cp310-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 7b962538e109e70f8c7232c7518f95a9d850650f635cdc6f7822a9046e928d14
MD5 75462cff23eb229a71c6821c24261b60
BLAKE2b-256 95774126c877f47c3803ff046f9c7a689b5a716af1a9c5b6221552d1d21adf8c

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for watchfiles-0.12-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2600b259dc216d7121b51f799b80cbb27443920cf91d74c87a0d397659179f92
MD5 e01164952a5d5ba4df02613802e6a779
BLAKE2b-256 a847295066c7774c7defae63fb544ef5c8282cba144dcc80b49e004fc2a59eca

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for watchfiles-0.12-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 25448386a9460f513a0f45289c9ccd72f5d2a67c88807ee39d28628472a70bdf
MD5 949511e3bc097c4d3280e2c0f05c7b11
BLAKE2b-256 dac8dcca05867e4f4144e0f7ee2c1ba30c117646cfd7331069febfcb86901d39

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: watchfiles-0.12-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 204.9 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.2

File hashes

Hashes for watchfiles-0.12-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 3baa80003ec031c583c906a618868712c8d9753a66087479d8f7d3fe56cf571b
MD5 626fa7f4fb8ac3a4159b287393ec01f3
BLAKE2b-256 3f4e522953e7c0d8515bc98eb936ff37f6a5e5e4e855eea63560edca755c2112

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp39-cp39-win32.whl.

File metadata

  • Download URL: watchfiles-0.12-cp39-cp39-win32.whl
  • Upload date:
  • Size: 198.8 kB
  • Tags: CPython 3.9, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.2

File hashes

Hashes for watchfiles-0.12-cp39-cp39-win32.whl
Algorithm Hash digest
SHA256 0dde3b3f849ea7fcddf6f57324925c814a01fb59626711463efd714614a1f8b7
MD5 f2f8deddf15c423422f1bc2a993f1493
BLAKE2b-256 7ccb890ac5b59e1e19b8161d379731991cd6fb3bde662576babc14d5694e22a7

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp39-cp39-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for watchfiles-0.12-cp39-cp39-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 4a6bc1516265994b4ada1a14906fa4f4d2cdf43b19dd886da5a9f9a1c576b94a
MD5 538390428bcf0e0d738751811485d3a8
BLAKE2b-256 1ea5469e377dc9dc339d58a19afdcbc5962d84846ba41db2b2b1f10036f17b4b

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp39-cp39-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for watchfiles-0.12-cp39-cp39-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 34ee9a6fea8778688f2eebede9b57467fc44ac252d7f59b575b701b6dc836f4e
MD5 3aa708d24fa7bb2f8f4bfd6872b75222
BLAKE2b-256 c082e7113e38ed016925e822e9954053d0acc84c51f6f609d180bf7b735f0937

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for watchfiles-0.12-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 234010bb28dc8258184f289d30385a6255c9e84e0001d84ad357f3cc0cf79046
MD5 2d4820fd9a5d72afd719bf0b22b2b1d2
BLAKE2b-256 ebd732f38e61a4b28e3b0510db1bdfd25e49b9f299fdf0c2cae515a6ed0f28bd

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for watchfiles-0.12-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 5de85d010085479b3a02f7b33c0fb5307e87ba95afaf308623bc61d0849741ee
MD5 8edebfde12489fa4b529436b5b65940a
BLAKE2b-256 e8696dd919f336de27e1412ed504c2154b138e5b347f0565fbf183cb8fcb9d21

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: watchfiles-0.12-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 204.4 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.2

File hashes

Hashes for watchfiles-0.12-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 bef776fb46460592d8eecd0028f60dd39953bc911731f25dbc2cd9c66adbe6a3
MD5 16bd8f7435cb008169bff0fd559c6040
BLAKE2b-256 0ae4e632b1b9e65073c7983ff3325c25e06ba3dd05a0fb6ef84864c1bf69fc20

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp38-cp38-win32.whl.

File metadata

  • Download URL: watchfiles-0.12-cp38-cp38-win32.whl
  • Upload date:
  • Size: 198.6 kB
  • Tags: CPython 3.8, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.2

File hashes

Hashes for watchfiles-0.12-cp38-cp38-win32.whl
Algorithm Hash digest
SHA256 57aa5399d9a71843b920da6166dc8396254ac6acb6266c15d4a63d060d78dff3
MD5 ca16b40a407efb92439d268a4e8dc05f
BLAKE2b-256 fdb7f83ff2c4b3e60789b840082285cc4b5fd9059d356f6aeb84376dac41268a

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp38-cp38-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for watchfiles-0.12-cp38-cp38-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 0f0e6c4ea5d131495f2404021eefb5d5e774c11877456b35fc645a1e9a6c1e98
MD5 f83bb04ebe645b54027d1da0c3c1e6d2
BLAKE2b-256 f3c3afc840dcf6794f949ecedccec25f42d56b8a906323706076c81dda723a06

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp38-cp38-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for watchfiles-0.12-cp38-cp38-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 1a8ab59b81f86ce926df80396ea1b79e441fb10aed069656f21f1d429691953d
MD5 b3e17099f8849635aeb43512975e3386
BLAKE2b-256 42995a66f8ae89487d4118da2e9e9b9be3e8c3239fca2a13e1a5df9ed0baffc4

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for watchfiles-0.12-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c9a06ba08f0ca0dcc8bb79ce213a079a11a60808dd464c1940af9bcaba5bba48
MD5 cf5fca4fe1c3137d2836594ab6f8e9dd
BLAKE2b-256 daa02b7cef49500ee607a2b8fd815af4f9ed65c3c71fcfffcd209817faada913

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for watchfiles-0.12-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8b33d4140c7af67d08cd6443b26cd8f2dd41ccc0f59c2f6f37a285f7b6bb3412
MD5 e9941f41c9988a14fd4d8438e1d2b956
BLAKE2b-256 97797f1c79b809eed4a71f74c260d64ffd7e136f4efbe2e3d3defdda0c1b2336

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: watchfiles-0.12-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 204.3 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.2

File hashes

Hashes for watchfiles-0.12-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 5ae8dc1d3ad268e793f98a06e7c7239697a9f96e168b6497f597bfad1dd835c9
MD5 9e47678989e9a3bc83ff439269866daa
BLAKE2b-256 e5512c6e632a21cc4602f1ec7926371fbd31375be1a04dbc255da4d516550c9f

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp37-cp37m-win32.whl.

File metadata

  • Download URL: watchfiles-0.12-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 198.5 kB
  • Tags: CPython 3.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.2

File hashes

Hashes for watchfiles-0.12-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 84f1ab85515cb9f2fc8d775d2658d7d9fd83878a7074862810400c392bce795a
MD5 cfc5f6acc03cb024a3d4da0ab4da7a32
BLAKE2b-256 a69e6489b31964a96dc16223044ab7800a94f85e5f744bc6e70b413ef3a64716

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp37-cp37m-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for watchfiles-0.12-cp37-cp37m-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 070b4405b3f6fb8efbd6920c9b185f643f19abda5af44fe861ea942bf4198567
MD5 16d6347f905362ca64ae62b7614c96d1
BLAKE2b-256 ff93050753ceccbb7abfa14520e22943a252f3d6de2313bcc386f16c295a1e64

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp37-cp37m-manylinux_2_24_i686.whl.

File metadata

File hashes

Hashes for watchfiles-0.12-cp37-cp37m-manylinux_2_24_i686.whl
Algorithm Hash digest
SHA256 b979461efb0950bdf73467e0ce556cc8243b74089218da282715a3147d96ba54
MD5 a08df90ff2cd26af3979317b8069965b
BLAKE2b-256 6fb1b46cb212b441a26f6546125774426655a9c6d0dbc0982fbb36e23b98f69d

See more details on using hashes here.

Provenance

File details

Details for the file watchfiles-0.12-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for watchfiles-0.12-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e0d03db135f9827d76b53684b2d6fd8f916b55f00ae31e3b351494616c276dfe
MD5 13fcd4e6f47e024691a94f627be51839
BLAKE2b-256 78cce5d5e6a7421721e7105ce2eeae3223290a3b6598ecdb313f5ec38936f6a1

See more details on using hashes here.

Provenance

Supported by

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