ApiPyloid
PyloidRPC
PyloidRPC based on HTTP, easy function calls between the frontend (web) and backend (Python)
Initialization
PyloidRPC
The main RPC class that manages method registration and handles RPC calls.
from pyloid.rpc import PyloidRPC
server = PyloidRPC()
Method Decorator
method
The decorator to register a method to the RPC server.
@server.method()
def greet(name: str):
return f"Hello, {name}!"
Parameters
Prop
Type
RPCContext
RPCContext
The RPCContext
class provides access to the current application state when a parameter named ctx
is present in your method signature.
Properties
RPCContext.pyloid
: The Pyloid application instanceRPCContext.window
: The current browser window instance
Basic Usage
Simple RPC Method
from pyloid.rpc import PyloidRPC
server = PyloidRPC()
@server.method()
async def greet(name: str):
return f"Hello, {name}!"
RPC Method with Context
from pyloid.rpc import PyloidRPC, RPCContext
server = PyloidRPC()
@server.method()
async def create_google_window(ctx: RPCContext):
window = ctx.pyloid.create_window(title="Google")
window.load_url("https://www.google.com")
window.show_and_focus()
Toggle Fullscreen Example
from pyloid.rpc import PyloidRPC, RPCContext
server = PyloidRPC()
@server.method()
async def toggle_fullscreen(ctx: RPCContext):
# Access the current window through context
is_fullscreen = ctx.window.is_fullscreen()
ctx.window.set_fullscreen(not is_fullscreen)
return not is_fullscreen
Error Handling
RPCError
Pyloid provides a custom RPCError
class for handling application-specific errors:
from pyloid.rpc import PyloidRPC, RPCError
server = PyloidRPC()
@server.method()
async def divide(a: float, b: float):
if b == 0:
# Create a custom error with code and message
raise RPCError("Division by zero is not allowed", code=-32000)
return a / b
Connecting RPC to Windows
Window Integration
from pyloid import Pyloid
from pyloid.rpc import PyloidRPC
# Create an RPC server instance
server = PyloidRPC()
@server.method()
async def greet(name: str):
return f"Hello, {name}!"
# Connect the RPC server instance
app = Pyloid(app_name="MyApp", single_instance=True, server=server)
Frontend RPC Calls
Once your RPC server is running, you can call your methods from the frontend
import { rpc } from 'pyloid-js';
// Call the 'greet' RPC function to receive a greeting.
rpc
.call('greet', { name: 'Alice' })
.then((response) => {
console.log(response); // Outputs "Hello, Alice!"
})
.catch((error) => {
console.error('Error occurred during RPC call:', error);
});
Notes
- All RPC functions must be defined as async, and you can use await inside them.
- When calling RPC functions from the frontend, simply pass the function name and parameters as they are.
- 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 server.