Webview¶
Custom HTML panels for rich UI within VS Code.
WebviewPanel
¶
Represents a VS Code webview panel.
A webview panel displays custom HTML content in a VS Code editor tab.
Initialize a WebviewPanel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
VSCodeClient
|
The VS Code client instance |
required |
panel_id
|
str
|
Unique identifier for this panel |
required |
view_type
|
str
|
Identifier for the webview type |
required |
title
|
str
|
Title displayed in the editor tab |
required |
Source code in vscode_sockpuppet/webview.py
update_html
¶
Update the HTML content of the webview.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
html
|
str
|
The new HTML content to display |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the panel has been disposed |
Source code in vscode_sockpuppet/webview.py
update_title
¶
Update the title of the webview panel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title
|
str
|
The new title to display |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the panel has been disposed |
Source code in vscode_sockpuppet/webview.py
update_icon
¶
Update the icon of the webview panel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
icon_path
|
str
|
Absolute path to the icon file |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the panel has been disposed |
Source code in vscode_sockpuppet/webview.py
post_message
¶
Post a message to the webview's JavaScript context.
The message can be received in the webview's JavaScript using: window.addEventListener('message', event => { const message = event.data; // Handle message });
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Any
|
The message to send (must be JSON-serializable) |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the panel has been disposed |
Source code in vscode_sockpuppet/webview.py
as_webview_uri
¶
Convert a local file URI to a webview URI.
Webviews cannot directly load resources from the workspace or local file system using file: URIs. This method converts a local file: URI into a URI that can be used inside the webview to load the same resource.
The local resource must be in a directory listed in localResourceRoots when creating the webview, otherwise it cannot be loaded.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
local_uri
|
str
|
Local file URI (e.g., 'file:///path/to/file.png') or absolute file path |
required |
Returns:
Type | Description |
---|---|
str
|
Webview URI that can be used in webview HTML |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the panel has been disposed |
Example
Convert a local file to webview URI¶
img_uri = panel.as_webview_uri('file:///path/to/image.png')
Use in HTML¶
html = f''
panel.update_html(html)
Source code in vscode_sockpuppet/webview.py
on_did_receive_message
¶
Subscribe to messages posted from the webview's JavaScript.
The handler will be called whenever the webview posts a message using: const vscode = acquireVsCodeApi(); vscode.postMessage({ your: 'data' });
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler
|
Callable[[Any], None]
|
Callback function that receives the message data |
required |
Returns:
Type | Description |
---|---|
Callable[[], None]
|
A function that can be called to unsubscribe the handler |
Example
def handle_message(message): print(f"Received: {message}")
Subscribe¶
unsubscribe = panel.on_did_receive_message(handle_message)
Later, to unsubscribe¶
unsubscribe()
Source code in vscode_sockpuppet/webview.py
on_did_dispose
¶
Subscribe to the panel disposal event.
The handler will be called when the webview panel is disposed, either by calling dispose() or when the user closes the panel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler
|
Callable[[], None]
|
Callback function called when the panel is disposed |
required |
Returns:
Type | Description |
---|---|
Callable[[], None]
|
A function that can be called to unsubscribe the handler |
Example::
def on_dispose():
print("Webview was closed")
# Subscribe
unsubscribe = panel.on_did_dispose(on_dispose)
# Later, to unsubscribe
unsubscribe()
Source code in vscode_sockpuppet/webview.py
on_did_change_view_state
¶
Subscribe to view state change events.
The handler will be called when the panel's visibility or active state changes (e.g., when the user switches tabs or focuses the panel).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler
|
Callable[[dict], None]
|
Callback function that receives a dict with 'visible' and 'active' keys |
required |
Returns:
Type | Description |
---|---|
Callable[[], None]
|
A function that can be called to unsubscribe the handler |
Example::
def on_view_state_change(state):
print(f"Visible: {state['visible']}, Active: {state['active']}")
if state['active']:
print("Panel is now in focus!")
# Subscribe
unsubscribe = panel.on_did_change_view_state(on_view_state_change)
# Later, to unsubscribe
unsubscribe()
Source code in vscode_sockpuppet/webview.py
dispose
¶
Dispose of the webview panel, closing it in VS Code.
After disposal, the panel cannot be used anymore. This will trigger any registered on_did_dispose handlers.
Source code in vscode_sockpuppet/webview.py
__enter__
¶
WebviewOptions¶
WebviewOptions
¶
WebviewOptions(enable_scripts: bool = True, retain_context_when_hidden: bool = False, local_resource_roots: Optional[list[str]] = None)
Options for configuring a webview panel.
Initialize webview options.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
enable_scripts
|
bool
|
Whether to enable JavaScript in the webview |
True
|
retain_context_when_hidden
|
bool
|
Whether to keep the webview's context when hidden |
False
|
local_resource_roots
|
Optional[list[str]]
|
List of URI paths that the webview can load local resources from |
None
|
Source code in vscode_sockpuppet/webview.py
to_dict
¶
Convert options to a dictionary for JSON serialization.