Environment
This should work on Domino 4 and Domino 5 Environments. Tested on Domino 5.
Issue
The example code for Dash works as an App but when I try to use it inside a Workspace, it doesn't work!
Can you help me run App inside a Workspace? This does not work smoothly.
Can we combine the code for Apps to work both as App and inside a Workspace?
Resolution
Since the URL for Apps and JupyterLab Proxy is slightly different, and the port 8888 is already used by a Workspace, the code would need to be constantly changed between testing and deployment OR you'll use the environment or try: commands in Python.
One code which would work if you use JupyterLab:
from dash import Dash, dash_table
import pandas as pd
import os
# Configure Dash to recognize the URL of the container
user = os.environ.get("DOMINO_PROJECT_OWNER")
project = os.environ.get("DOMINO_PROJECT_NAME")
runid = os.environ.get("DOMINO_RUN_ID")
jupyter = os.environ.get("JUPYTER_SERVER_ROOT")
# If this variable exists, we're running a Workspace
if jupyter == '/':
debug=False
port=8889
runurl = '/' + user + '/' + project + '/notebookSession/' + runid + '/proxy/8889/'
else:
debug=True
port=8888
runurl = '/' + user + '/' + project + '/r/notebookSession/' + runid + '/'
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/solar.csv')
app = Dash(__name__, routes_pathname_prefix='/', requests_pathname_prefix=runurl)
app.layout = dash_table.DataTable(df.to_dict('records'), [{"name": i, "id": i} for i in df.columns])
if __name__ == '__main__':
app.run_server(port=port, host='0.0.0.0', debug=debug)
A separate method would be to separate into try: and except: which also allows you to split up the execution code depending on App/Workspace:
from dash import Dash, dash_table
import pandas as pd
import os
# Configure Dash to recognize the URL of the container
user = os.environ.get("DOMINO_PROJECT_OWNER")
project = os.environ.get("DOMINO_PROJECT_NAME")
runid = os.environ.get("DOMINO_RUN_ID")
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/solar.csv')
try:
runurl = '/' + user + '/' + project + '/r/notebookSession/' + runid + '/'
app = Dash(__name__, routes_pathname_prefix='/', requests_pathname_prefix=runurl)
app.layout = dash_table.DataTable(df.to_dict('records'), [{"name": i, "id": i} for i in df.columns])
if __name__ == '__main__':
app.run_server(port=8888, host='0.0.0.0', debug=True)
except:
runurl2 = '/' + user + '/' + project + '/notebookSession/' + runid + '/proxy/8889/'
app2 = Dash(__name__, routes_pathname_prefix='/', requests_pathname_prefix=runurl2)
app2.layout = dash_table.DataTable(df.to_dict('records'), [{"name": i, "id": i} for i in df.columns])
if __name__ == '__main__':
app2.run_server(port=8889, host='0.0.0.0', debug=False)
Important note
Using debug=False when running inside a Workspace appears needed in Domino 5.
Comments
0 comments
Please sign in to leave a comment.