### Install watchfiles using pip Source: https://github.com/samuelcolvin/watchfiles/blob/main/docs/index.md Install the watchfiles package from PyPI using pip. This is the standard installation method. ```bash pip install watchfiles ``` -------------------------------- ### Rust backend example Source: https://github.com/samuelcolvin/watchfiles/blob/main/docs/api/rust_backend.md Instantiate and use RustNotify directly. Ensure to call `close` when finished to release resources. ```python from watchfiles._rust_notify import RustNotify r = RustNotify(['first/path', 'second/path'], False, False, 0, True, False) changes = r.watch(1_600, 50, 100, None) print(changes) r.close() ``` -------------------------------- ### Install watchfiles using conda-forge Source: https://github.com/samuelcolvin/watchfiles/blob/main/docs/index.md Install the watchfiles package from the conda-forge channel using conda or mamba. This is an alternative installation method. ```bash mamba install -c conda-forge watchfiles ``` -------------------------------- ### foobar.py Example Source: https://github.com/samuelcolvin/watchfiles/blob/main/docs/cli.md This Python script defines a simple aiohttp web server that displays recent file changes. It's designed to be run and reloaded by the watchfiles CLI. ```python title="foobar.py" 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) ``` -------------------------------- ### Rust backend context manager example Source: https://github.com/samuelcolvin/watchfiles/blob/main/docs/api/rust_backend.md Utilize RustNotify as a context manager for automatic resource management. The `close` method is implicitly called upon exiting the `with` block. ```python from watchfiles._rust_notify import RustNotify with RustNotify(['first/path', 'second/path'], False, False, 0, True, False) as r: changes = r.watch(1_600, 50, 100, None) print(changes) ``` -------------------------------- ### watchfiles CLI Help Source: https://github.com/samuelcolvin/watchfiles/blob/main/docs/cli.md Display the help message for the watchfiles CLI to see all available options and commands. ```bash watchfiles --help ``` -------------------------------- ### Basic CLI Usage Source: https://github.com/samuelcolvin/watchfiles/blob/main/docs/cli_help.txt Shows the basic structure for using the watchfiles CLI to execute a Python function on file changes. ```bash watchfiles foobar.main ``` -------------------------------- ### CLI Usage with Filter and Shell Command Source: https://github.com/samuelcolvin/watchfiles/blob/main/docs/cli_help.txt Demonstrates how to use the watchfiles CLI to watch specific file types (Python) in multiple directories and execute a shell command. ```bash watchfiles --filter python 'pytest --lf' src tests ``` -------------------------------- ### Running a Shell Command with watchfiles Source: https://github.com/samuelcolvin/watchfiles/blob/main/docs/cli.md Execute a shell command and have it automatically reloaded on file changes. Useful for tasks like running tests. ```bash watchfiles 'pytest --lf' ``` -------------------------------- ### Run a command with watchfiles CLI Source: https://github.com/samuelcolvin/watchfiles/blob/main/README.md Execute a command and automatically reload it when files in specified directories change. This is a command-line interface usage. ```bash watchfiles "some command" src ``` -------------------------------- ### Watch Files Synchronously Source: https://github.com/samuelcolvin/watchfiles/blob/main/docs/index.md Use `watch` to monitor a directory for file changes synchronously. It yields sets of changes detected. ```python from watchfiles import watch for changes in watch('./path/to/dir'): print(changes) ``` -------------------------------- ### Run Process with Synchronous Reload Source: https://github.com/samuelcolvin/watchfiles/blob/main/docs/index.md Use `run_process` to execute a target function and automatically reload it when file changes are detected. This is a synchronous operation. ```python from watchfiles import run_process def foobar(a, b, c): ... if __name__ == '__main__': run_process('./path/to/dir', target=foobar, args=(1, 2, 3)) ``` -------------------------------- ### Watch Files Asynchronously Source: https://github.com/samuelcolvin/watchfiles/blob/main/docs/index.md Use `awatch` for asynchronous file watching. This is suitable for use within asyncio applications. ```python import asyncio from watchfiles import awatch async def main(): async for changes in awatch('/path/to/dir'): print(changes) asyncio.run(main()) ``` -------------------------------- ### Custom Filter for Web Files Source: https://github.com/samuelcolvin/watchfiles/blob/main/docs/api/filters.md Extends DefaultFilter to include only changes to specified web file extensions (.html, .css, .js). Requires importing Change, DefaultFilter, and watch. ```python 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) ``` -------------------------------- ### Custom Filter for Added Files Source: https://github.com/samuelcolvin/watchfiles/blob/main/docs/api/filters.md A simple callable filter that only includes changes when a new file is added. Requires importing Change and watch. ```python 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) ``` -------------------------------- ### Run Process with Asynchronous Reload Source: https://github.com/samuelcolvin/watchfiles/blob/main/docs/index.md Use `arun_process` to execute a target function asynchronously and automatically reload it when file changes are detected. This is suitable for asyncio applications. ```python 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()) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.