diff --git a/app/my_app.py b/app/my_app.py new file mode 100644 index 0000000000000000000000000000000000000000..db083be592f45c76bd6c05d3f2783614b84cfcb0 --- /dev/null +++ b/app/my_app.py @@ -0,0 +1,70 @@ +import sys +sys.path.append("/home/jovyan/") + +import argparse +import logging + +from urllib.parse import urlparse, urljoin + +from dash import Dash + +from jupyter_server.serverapp import list_running_servers + +try: + from my_layout import layout + from my_callbacks import register_callbacks +except ModuleNotFoundError: + # do not let Dash start + exit() + + +logging.basicConfig(level=logging.INFO) + +# weird trick to find base_url for the jupyterlab +def find_jupyterlab_base_url(): + servers = list_running_servers() + for server in servers: + if server["port"] == 8888: + return server['url'] + return None + + +# get the correct port from proxy +parser = argparse.ArgumentParser() +parser.add_argument("--port", type=int) +args = parser.parse_args() +port: int = args.port + +if not port: + raise ValueError(f"Port of proxy server for Dash not found in {args}.") +else: + logging.debug(f"Dash app running on port {port}.") + + +base_url = find_jupyterlab_base_url() +if base_url is None: + raise ValueError("Base URL of Jupyterlab could not be detected.") +logging.debug(f"Base URL: {base_url}") + +proxy_base_path = urlparse(urljoin(base_url + "/", f"proxy/{port}/")).path +logging.debug(f"Proxy base path: {proxy_base_path}") + +# define Dash app +app = Dash( + name=__name__, + requests_pathname_prefix=proxy_base_path +) + +# define layout +app.layout = layout + +# register all callback functions +register_callbacks(app=app) + +# Run Dash app in the notebook +app.run( + jupyter_mode="jupyterlab", + port=port, + host="0.0.0.0", + debug=True +) diff --git a/dash_proxy/my_app_proxy.py b/dash_proxy/my_app_proxy.py new file mode 100644 index 0000000000000000000000000000000000000000..e91a8649001749f630ee85b4e6706f99049fb634 --- /dev/null +++ b/dash_proxy/my_app_proxy.py @@ -0,0 +1,16 @@ +def setup_my_app_proxy(): + command = [ + 'python', + '/dash/app/my_app.py', + '--port', + '{port}' + ] + + return { + "command": command, + "new_browser_tab": False, + "launcher_entry": { + "enabled": True, + 'title': 'MyApp' + } + } diff --git a/dash_proxy/setup.py b/dash_proxy/setup.py index ccd874ca87acf8f35bea40e0eb6e883150528e3f..8e1ebf5761c3de7f783ce204b25474e10f2b0d8d 100644 --- a/dash_proxy/setup.py +++ b/dash_proxy/setup.py @@ -5,11 +5,12 @@ setuptools.setup( author_email="julian.rasch@fh-muenster.de", description="A small module to run Dash inside a dockerized Jupyterlab.", name="jupyter-dash-proxy", - py_modules=["dash_proxy"], + py_modules=["dash_proxy", "my_app_proxy"], entry_points={ "jupyter_serverproxy_servers": [ # name = packagename:function_name "Dash = dash_proxy:setup_dash_proxy", + "MyApp = my_app_proxy:setup_my_app_proxy" ] }, install_requires=["jupyter-server-proxy==4.0.0"],