Skip to content

Commit

Permalink
feat(py-client): enable cross trame-server communication
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Apr 25, 2023
1 parent ab55187 commit afab046
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ keywords =
packages = find:
include_package_data = True
install_requires =
trame-server>=2.9.0
trame-server>=2.11.0
trame-client>=2.1.0
trame-router==2.0.1
trame-components==2.1.0
Expand Down
32 changes: 31 additions & 1 deletion trame/app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from trame_server import Server
from trame_server import Server, Client
from trame_client.widgets.core import VirtualNode

# Ensure this is imported so that mimetypes.init() is decorated
import trame.app.mimetypes # noqa: F401

DEFAULT_NAME = "trame"
AVAILABLE_SERVERS = {}
AVAILABLE_CLIENTS = {}


def get_server(name=None, create_if_missing=True, **kwargs):
Expand Down Expand Up @@ -44,6 +45,35 @@ def get_server(name=None, create_if_missing=True, **kwargs):
return None


def get_client(url=None, hot_reload=False, **kwargs):
"""
Return a client to a remote trame applications.
If a url is given and such client is not available yet,
it will be created otherwise the previously created instance will be returned.
:param url: Websocket URL which to connect to.
:type url: str
:param hot_reload: Enable when state change function should be hot reloaded.
:type hot_reload: bool
:param **kwargs: any extra keyword args use for authentication configuration.
:return: Return a unique Client instance per given url. Each instance need to a connect() call.
:rtype: trame_server.client.Client
"""
if url in AVAILABLE_CLIENTS:
return AVAILABLE_CLIENTS[url]

client = Client(url=url, config=kwargs, hot_reload=hot_reload)
if url is not None:
AVAILABLE_CLIENTS[url] = client

return client


__all__ = [
"get_server",
"get_client",
]

0 comments on commit afab046

Please sign in to comment.