server

class trame_server.core.Server(name='trame', vn_constructor=None, **options)

Server implementation for trame. This is the core object that manage client/server communication but also holds a state and controller instance. With trame a server instance should be retrieved by using trame.app.get_server()

Known options:
  • log_network: False (path to log file)

  • ws_max_msg_size: 10000000 (bytes)

  • ws_heart_beat: 30

  • desktop_debug: False

Parameters:
  • name (str, optional (default: trame)) – A name identifier for a given server

  • **options

    Gather any keyword arguments into options

enable_module(module, **kwargs)

Expend server using a module definition which can be used to serve custom client code or assets, load/initialize resources (js, css, vue), register custom protocols and even execute custom code.

Any previously seem module will be automatically skipped.

The attributes that are getting processed in a module are the following:
  • setup(server, **kwargs): Function called first

  • scripts = [] : List all JavaScript URL that should be loaded

  • styles = [] : List all CSS URL that should be loaded

  • vue_use = [‘libName’, (‘libName2’, { **options })]: List Vue plugin to load

  • state = {} : Set of variable to add to state

  • server = { data: ‘/path/on/fs’ }: Set of endpoints to server static content

  • www = ‘/path/on/fs’ : Path served as main web content

Parameters:
  • module – A module to enable

  • kwargs – Any optional parameters needed for your module setup() function.

js_call(ref: str | None = None, method: str | None = None, *args)

Python call method on JS element.

Parameters:
  • ref (str) – ref name of the widget element

  • method (str) – name of the method that should be called

  • *args

    set of parameters needed for the function

change(*_args, **_kwargs)

Use as decorator @server.change(key1, key2, …) so the decorated function will be called like so _fn(**state) when any of the listed key name is getting modified from either client or server.

Parameters:

*_args

A list of variable name to monitor

trigger(name)

Use as decorator @server.trigger(name) so the decorated function will be able to be called from the client by doing click=”trigger(name)”.

Parameters:

name (str) – A name to use for that trigger

trigger_name(fn)

Given a function this method will register a trigger and returned its name. If manually registered, the given name at the time will be returned.

Returns:

The trigger name for that function

Return type:

str

property name

Name of server

property options

Server options provided at instantiation time

property cli

argparse parser

property state
Returns:

The server shared state

Return type:

trame_server.state.State

property controller
Returns:

The server controller

Return type:

trame_server.controller.Controller

property ui
Returns:

The server VirtualNode manager

Return type:

trame_server.ui.VirtualNodeManager

property running

Return True if the server is currently starting or running.

property ready

Return a future that will resolve once the server is ready

get_server_state()

Return the current server state

add_protocol_to_configure(configure_protocol_fn)

Register function that will be called with a wslink.ServerProtocol when the server start and is ready for registering new wslink.Protocol.

Parameters:

configure_protocol_fn – A function to be called later with a wslink.ServerProtocol as argument.

property protocol

Return the server root protocol

protocol_call(method, *args, **kwargs)

Call a registered protocol method

Parameters:
  • method (str) – Method registration name

  • *args

    Set of args to use for that method call

  • **kwargs

    Set of keyword arguments to use for that method call

Returns:

transparently return what the called function returns

start(port: int | None = None, thread: bool = False, open_browser: bool = True, show_connection_info: bool = True, disable_logging: bool = False, backend: str = 'aiohttp', exec_mode: str = 'main', timeout: int | None = None, host: str | None = None, **kwargs)

Start the server by listening to the provided port or using the –port, -p command line argument. If the server is already starting or started, any further call will be skipped.

When the exec_mode=”main” or “desktop”, the method will be blocking. If exec_mode=”task”, the method will return a scheduled task. If exec_mode=”coroutine”, the method will return a coroutine which will need to be scheduled by the user.

Parameters:
  • port – A port number to listen to. When 0 is provided the system will use a random open port.

  • thread – If the server run in a thread which means we should disable interuption listeners

  • open_browser – Should we open the system browser with app url. Using the –server command line argument is similar to setting it to False.

  • show_connection_info – Should we print connection URL at startup?

  • disable_logging – Ask wslink to disable logging

  • exec_mode – main/desktop/task/coroutine specify how the start function should work

  • timeout – How much second should we wait before automatically stopping the server when no client is connected. Setting it to 0 will disable such auto-shutdown.

  • host – The hostname used to bind the server. This can also be set with the environment variable TRAME_DEFAULT_HOST. Defaults to 'localhost'.

  • **kwargs

    Keyword arguments for capturing optional parameters for wslink server and/or desktop browser

async stop()

Coroutine for stopping the server

property port

Once started, you can retrieve the port used