This has stopped the Jupyter notebook running on that instance, freeing the
port for our application.

Let’s start by creating a directory in the project workspace:

$ mkdir -p /project/dash-example

Let’s now write the code for our application. We will create an application
that predicts whether someone is a cat-person or a dog-person based on their
name. Create a file called app.py in /project/dash-example, with the
following contents:

importdashimportdash_core_componentsasdccimportdash_html_componentsashtmlfromdash.dependenciesimportInput,Outputapp=dash.Dash('Plotly Dash on SherlockML',url_base_pathname='/',csrf_protect=False)app.layout=html.Div(children=[html.H1(children='Are you a cat person?'),html.Label('Your name: '),dcc.Input(id='input-div'),html.Div(id='output-div',children=[])])@app.callback(Output(component_id='output-div',component_property='children'),[Input(component_id='input-div',component_property='value')])defupdate_output(input_value):ifinput_valueisNoneornotinput_value:return['You have not typed your name yet.']ifinput_value=='Heisenberg':return['You are a cat person.']else:return['You are a dog person.']if__name__=='__main__':app.run_server(host='127.0.0.1',port=8888,debug=True)

This code defines a minimal application that listens on port 8888, the port
that we have freed by stopping Jupyter. Let’s now start our app:

$ cd /project/dash-example
$ python app.py

Note

Running the app with pythonapp.py runs the app with its development
server in debug mode (see app.run_server() in the example code above),
which provides nice features like automatic reloading of the app when the
code is changed.

However, this is not suitable for use in deployed applications, where we
instead use gunicorn, a production-ready Python HTTP server. If you want to
run the app in the same way as it will be run in production, install
gunicorn and gevent from pip (pipinstallgunicorngevent) and run the
app with:

If you now open your server from the servers section of the workspace:

You will see your application!

Carry on developing your app. When you save changes to your code, the app will
automatically reload itself (unless you are running it with gunicorn, in which
case you will need to first stop it by typing Ctrl-C in the terminal in which
you started the app, and restart it by running the same command).

You have now developed a great dashboard, and you want to let other members of
your project access it. SherlockML supports hosting Plotly Dash applications.
Head to the Deployments page in SherlockML, and in the Apps tab click
the + button above the tab to create a new app. You will be prompted to
enter a name and domain for your app. Select Plotly Dash for Type.

Click Create App. You will then be taken to the App Settings page.

You will need to make the following changes to the application settings:

Change the working directory to /project/dash-example.

Change the python module to app. This should be the name of the file
containing the app, without .py.

Change the python object to app.server. This should be the name of the
Python variable that gunicorn will serve.

Save your application by clicking the Save button, then click Start app to
start your Plotly Dash server. After a few seconds, you will see the
status of your app change to Running. At this point, a URL will appear.
Select that URL and place it in your browser search bar. You will be taken to
the application! Behind the scenes, SherlockML verifies that you are at least
an observer in the project that the app belongs to. While your app is
deployed, you can monitor its logs in the Monitor tab.

Plotly Dash applications are deployed using Gunicorn. You can leverage this to deploy any WSGI web framework. This lets you, in
particular, deploy Flask and Django applications.

Flask applications will work out of the box. Your application
will contain a file with a line like:

server=Flask(...)

When creating the deployment in SherlockML, choose the Python module
containing that line for python module. For instance, if that line
is in a file called app.py, the python module will be
app. Choose server as the python object.

For Django applications, you will need to create an environment that
installs django through pip. Your application should have a file
called <project_name>/wsgi.py containing the following lines: