Extras¶
Higher-level composite widgets built from the core pgwidgets primitives. These ship with pgwidgets but live in their own subpackage so the core import surface stays minimal.
Import individual modules as needed:
from pgwidgets.extras.file_browser import FileBrowser
FileBrowser¶
A file/folder selection dialog assembled from a Dialog,
TableView, TextEntry, and ComboBox. All filesystem I/O
runs on the Python side; the browser only renders the listing
returned by the server, so the dialog reads the server’s filesystem
(this is by design — it lets you build server-side tools that browse
server-side data).
Quick start¶
from pgwidgets.sync import Application
from pgwidgets.extras.file_browser import FileBrowser
app = Application()
@app.on_connect
def setup(session):
fb = FileBrowser(session, title="Open Image", mode="file")
fb.add_ext_filter("Images", "png")
fb.add_ext_filter("Images", "jpg")
fb.add_ext_filter("Images", "fits")
fb.set_directory("/data/images")
fb.on("activated", lambda path: print(f"Selected: {path}"))
fb.popup()
app.run()
Constructor¶
FileBrowser(
session,
title="Browse",
modal=True,
autoclose=True,
mode="file",
)
Parameter |
Description |
|---|---|
|
The pgwidgets |
|
Title shown in the dialog’s title bar. |
|
When |
|
When |
|
Selection mode. See Modes. |
Modes¶
Mode |
Behavior |
|---|---|
|
Pick a single existing file. Confirm button shows “Open”.
Double-click on a file selects it. |
|
Pick multiple existing files via the table’s multi-selection.
Confirm button shows “Open”. |
|
Pick a directory. Confirm button shows “Select”. Double-click
navigates into the folder; the user must press the confirm
button (or type a folder name and confirm) to select.
|
|
Pick a filename for saving. Confirm button shows “Save”. If
the typed name matches an existing file, a confirmation dialog
prompts to overwrite. |
Methods¶
Method |
Description |
|---|---|
|
Set the starting directory. If not called, the dialog opens
in the process’s current working directory the first time
|
|
Pre-fill the filename entry — useful for |
|
Change the selection mode ( |
|
Add a file-extension filter to the filter ComboBox at the
bottom of the dialog. Calling repeatedly with the same
fb.add_ext_filter("Images", "png")
fb.add_ext_filter("Images", "jpg")
# Filter combo shows: "Images (*.png, *.jpg)"
Matching is case-insensitive on both the filter and the
file extension, so a filter for |
|
Remove all extension filters. Returns the combobox to just “All Files”. |
|
Show the file browser dialog. |
|
Register a callback for |
|
Inherited from |
FileBrowser inherits from pgwidgets.callbacks.Callbacks, so
it has the full callback-registration API
(add_callback/on/remove_callback/clear_callback/
has_callback). See Callbacks base class below.
Callbacks base class¶
pgwidgets.callbacks.Callbacks is a tiny base class that gives
Python-side composite/utility classes the same callback API as a
real Widget, without making them widgets. Use it whenever you
build a Python-side helper that needs to expose handler registration
to user code (the way FileBrowser does).
from pgwidgets.callbacks import Callbacks
class MyTool(Callbacks):
def __init__(self):
super().__init__()
self.enable_callback("done")
def do_work(self):
...
self.make_callback("done", result)
tool = MyTool()
tool.add_callback("done", lambda obj, result: ...) # widget-arg style
tool.on("done", lambda result: ...) # plain style
API:
Method |
Description |
|---|---|
|
Declare |
|
True if the action has been enabled. |
|
Register a handler. Invoked as
|
|
Register a handler without the source-object first arg.
Invoked as |
|
Unregister all entries for action whose handler is handler. |
|
Remove every handler for action. |
|
Invoke every registered handler. Exceptions from one handler are logged and don’t prevent the rest from running. |
Icons¶
The dialog uses SVG icons from pgwidgets-js for files and folders
by default — they are rendered at a small size in the table column.
A module-level registry maps category names to data: URIs.
from pgwidgets.extras.file_browser import ICONS, set_icon
# Override the default file/folder icons
set_icon("file", "/path/to/my-file.svg")
set_icon("folder", "/path/to/my-folder.svg")
set_icon("parent", "/path/to/up-arrow.svg")
# Per-extension icons (key is a lowercase extension)
set_icon("py", "/path/to/python.svg")
set_icon("jpg", "/path/to/image.svg")
set_icon("png", "/path/to/image.svg")
The registered file is read once and embedded as a data: URI, so it
does not need to remain on disk after registration. PNG, JPEG, GIF,
and SVG are all supported (anything mimetypes recognises as an image
type, plus arbitrary types served as application/octet-stream).
Special category names:
"file"— default icon for any file with no extension-specific override."folder"— icon for directories."parent"— icon for the..parent-directory entry. Defaults to the same icon as"folder"— register a distinct one if you want a separate look.
Any other category key is treated as a lowercase file extension and
takes precedence over "file" when a file with that extension is
listed. Extension matching is case-insensitive (the registry is
keyed by lowercase extensions, and file names are lowercased before
lookup).
Module API¶
Symbol |
Description |
|---|---|
|
The dialog class (described above). |
|
The module-level |
|
Read an image file and register it under |