RPC
Pyloid is a Python-based desktop app framework that allows easy function calls between the frontend (web) and backend (Python) through an RPC (Remote Procedure Call) system. Below is a guide on how to use Pyloid's RPC system, along with various examples of utilizing app/window objects within RPC functions.
Basic Usage
Here's how to set up and use the RPC system:
Context Injection
Pyloid RPC automatically injects a RPCContext
object when a parameter named ctx
is present in your method signature. This context provides access to:
ctx.pyloid
: The Pyloid application instancectx.window
: The current browser window instance
This makes it easy to interact with the application state from your RPC methods.
RPC Method Decorator
Parameters
name
(str, optional): Custom name for the RPC method. If not provided, the function name will be used.
Requirements
The decorated function must be an async function
Function parameters can be typed and will be properly passed from JSON-RPC calls
Including a
ctx
parameter will automatically inject the context object
RPCContext Object
The RPCContext
class provides access to the current application state:
Example with Context
Advanced Error Handling
Pyloid provides a custom RPCError
class for handling application-specific errors:
Starting the RPC Server
Pyloid's RPC system does not require a separate server. Instead, you can connect the RPC instance to the window when creating it. When creating a window, pass the RPC instance to the rpc
parameter, and the window will automatically enable RPC functionality.
This method is particularly useful in Pyloid's multi-window environment. You can connect different RPC instances to each window or share a single RPC instance across multiple windows. When an RPC request is processed through a window, it can access the window and the application instance through the context (ctx
).
The RPC server also uses dynamic port allocation internally, so it will always start with an available port without port conflicts.
Calling RPC Methods from Frontend
Once your RPC server is running, you can call your methods from the frontend:
Complete Example
Here's a complete example that shows how to set up an RPC server with the static file server:
Notes
All RPC functions must be defined as async, and you can freely use await inside them.
When calling RPC functions from the frontend, simply pass the function name and parameters as they are.
Complex GUI control and app state management can also be easily implemented via RPC.
All GUI-related functions in Pyloid are designed to be thread-safe. Therefore, unlike typical Python GUI frameworks, you can safely call GUI-related methods (app, window, etc.) inside RPC functions (asynchronous/other threads) even if they are not on the main thread. Without any additional synchronization or queuing, you can freely control the GUI from RPC.
Last updated
Was this helpful?