Pyloid Docs
GithubLanguage
  • 💎What is Pyloid?
  • Getting Started
    • Prerequisites
    • Create Pyloid App
  • Core Concepts
    • Thread Safety
  • API
    • Python (Backend)
      • Pyloid
      • BrowserWindow
      • Monitor
      • TrayEvent
      • Utility Functions
      • RPC
      • Store
    • Javascript (Frontend)
      • Event
      • BaseAPI
      • RPC
  • Guides
    • Load Webview
    • Serve Frontend
    • Calling Python from JS
    • Calling JS from Python
    • Keyboard Shotcuts
    • Notification
    • Tray
    • Timer
    • File Watcher
    • Clipboard
    • Window Position
    • Devtools
    • Window Drag Region
    • Window Radius And Transparent
    • Autostart
    • Production Utils
    • Desktop Monitor
    • File Dialog
    • Splash Screen
Powered by GitBook
On this page
  • Basic Usage
  • Start Watching a File
  • Start Watching a Directory
  • Set File Change Callback
  • Set Directory Change Callback
  • Stop Watching
  • Check Watched Paths
  • Get Only Watched Files
  • Get Only Watched Directories
  • Remove All Watched Paths
  • Usage Example
  • Precautions

Was this helpful?

Edit on GitHub
  1. Guides

File Watcher

The File Watcher provides functionality to monitor changes in specific files or directories. This allows you to receive automatic notifications when files or directories are modified.

Basic Usage

Start Watching a File

To monitor changes in a specific file, use the following:

result = app.watch_file("path/file.txt")
if result:
    print("File watching started")
else:
    print("Failed to start file watching")
from pyloid import Pyloid

app = Pyloid(app_name="Pyloid-App", single_instance=True)

window = app.create_window(
    title="Pyloid Browser",
)

window.load_url("https://www.example.com")
window.show_and_focus()

result = app.watch_file("/path/file.txt")
if result:
    print("File watching started")
else:
    print("Failed to start file watching")

app.run()

Start Watching a Directory

To monitor changes in a specific directory, use the following:

result = app.watch_directory("/path/directory")
if result:
    print("Directory watching started")
else:
    print("Failed to start directory watching")
from pyloid import Pyloid

app = Pyloid(app_name="Pyloid-App", single_instance=True)

window = app.create_window(
    title="Pyloid Browser",
)

window.load_url("https://www.example.com")
window.show_and_focus()

result = app.watch_directory("/path/directory")
if result:
    print("Directory watching started")
else:
    print("Failed to start directory watching")

app.run()

Set File Change Callback

You can set a callback function to be executed when a file is changed:

def on_file_changed(path):
    print(f"File has been changed: {path}")

app.set_file_change_callback(on_file_changed)
from pyloid import Pyloid

app = Pyloid(app_name="Pyloid-App", single_instance=True)

window = app.create_window(
    title="Pyloid Browser",
)

window.load_url("https://www.example.com")
window.show_and_focus()

app.watch_file("/path/file.txt")

# Define callback function
def on_file_changed(path):
    print(f"File has been changed: {path}")

# Set callback function
app.set_file_change_callback(on_file_changed)

app.run()

Set Directory Change Callback

You can set a callback function to be executed when a directory is changed:

def on_directory_changed(path):
    print(f"Directory has been changed: {path}")

app.set_directory_change_callback(on_directory_changed)
from pyloid import Pyloid

app = Pyloid(app_name="Pyloid-App", single_instance=True)

window = app.create_window(
    title="Pyloid Browser",
)

window.load_url("https://www.example.com")
window.show_and_focus()

app.watch_directory("/path/directory")

# Define callback function
def on_directory_changed(path):
    print(f"Directory has been changed: {path}")

# Set callback function
app.set_directory_change_callback(on_directory_changed)

app.run()

Stop Watching

To stop watching a specific file or directory, use the following:

result = app.stop_watching("/path/to/file_or_directory")
if result:
    print("Watching stopped successfully")
else:
    print("Failed to stop watching")
from pyloid import Pyloid

app = Pyloid(app_name="Pyloid-App", single_instance=True)

window = app.create_window(
    title="Pyloid Browser",
)

window.load_url("https://www.example.com")
window.show_and_focus()


result = app.stop_watching("/path/to/file_or_directory")
if result:
    print("Watching stopped successfully")
else:
    print("Failed to stop watching")

app.run()

Check Watched Paths

To check all currently watched paths (files and directories), use the following:

watched_paths = app.get_watched_paths()
print("All watched paths:", watched_paths)
from pyloid import Pyloid

app = Pyloid(app_name="Pyloid-App", single_instance=True)

window = app.create_window(
    title="Pyloid Browser",
)

window.load_url("https://www.example.com")
window.show_and_focus()

watched_paths = app.get_watched_paths()
print("All watched paths:", watched_paths)

app.run()

Get Only Watched Files

watched_files = app.get_watched_files()
print("Watched files:", watched_files)
from pyloid import Pyloid

app = Pyloid(app_name="Pyloid-App", single_instance=True)

window = app.create_window(
    title="Pyloid Browser",
)

window.load_url("https://www.example.com")
window.show_and_focus()

watched_files = app.get_watched_files()
print("Watched files:", watched_files)

app.run()

Get Only Watched Directories

watched_directories = app.get_watched_directories()
print("Watched directories:", watched_directories)
from pyloid import Pyloid

app = Pyloid(app_name="Pyloid-App", single_instance=True)

window = app.create_window(
    title="Pyloid Browser",
)

window.load_url("https://www.example.com")
window.show_and_focus()

watched_directories = app.get_watched_directories()
print("Watched directories:", watched_directories)

app.run()

Remove All Watched Paths

app.remove_all_watched_paths()
print("All watched paths have been removed.")
from pyloid import Pyloid

app = Pyloid(app_name="Pyloid-App", single_instance=True)

window = app.create_window(
    title="Pyloid Browser",
)

window.load_url("https://www.example.com")
window.show_and_focus()

# Remove all watched paths
app.remove_all_watched_paths()
print("All watched paths have been removed.")

app.run()

Usage Example

Here's a comprehensive example of using the File Watcher:

# Start watching file and directory
app.watch_file("/path/to/file1.txt")
app.watch_file("/path/to/file2.txt")
app.watch_file("/path/to/file3.txt")
app.watch_file("/path/to/file4.txt")
app.watch_directory("/path/to/directory1")
app.watch_directory("/path/to/directory2")
app.watch_directory("/path/to/directory3")
app.watch_directory("/path/to/directory4")

# Define callback functions
def on_file_changed(path):
    print(f"File changed: {path}")

def on_directory_changed(path):
    print(f"Directory changed: {path}")

# Set callback functions
app.set_file_change_callback(on_file_changed)
app.set_directory_change_callback(on_directory_changed)

# Check watched paths
print("Watched paths:", app.get_watched_paths())
print("Watched files:", app.get_watched_files())
print("Watched directories:", app.get_watched_directories())

# Stop watching a specific path
app.stop_watching("/path/to/file1.txt")
app.stop_watching("/path/to/file2.txt")
app.stop_watching("/path/to/file3.txt")
app.stop_watching("/path/to/file4.txt")
app.stop_watching("/path/to/directory1")
app.stop_watching("/path/to/directory2")
app.stop_watching("/path/to/directory3")
app.stop_watching("/path/to/directory4")

# Remove all watched paths
app.remove_all_watched_paths()

Precautions

  • The File Watcher uses system resources, so use it only when necessary and clean up properly when finished.

  • Watching a large number of files or directories can impact system performance, so use caution.

  • Some file systems or operating systems may not detect certain types of changes.

This guide should help you understand the basic usage and advanced features of the File Watcher. If you have any additional questions or need further assistance, please don't hesitate to ask.

PreviousTimerNextClipboard

Last updated 6 months ago

Was this helpful?