SharedDASH / dash-demo.pyOpen in CoCalc
# Demo of Dash in a CoCalc project
# Dash tutorial:
#   https://dash.plot.ly/getting-started
# CoCalc HTTP server from a project
#   https://github.com/sagemathinc/cocalc/wiki/HTTPWebserver
#
# 1. Run this file from a .term:
#   python3 dash-demo.py
# 2. Open a browser window to
#   https://cocalc.com/<your project id>/server/9990
# i.e. if your project id is 30b9a512-6b2c-11e8-a361-4f5344355d2c, the link would be
#   https://cocalc.com/30b9a512-6b2c-11e8-a361-4f5344355d2c/server/9990
# You must be a collaborator on the project and logged into your CoCalc account
# to view the application.

import dash
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash()

app.layout = html.Div(children=[
    html.H1(children='Hello Dash from CoCalc'),

    html.Div(children='''
        Dash: A web application framework for Python.
    '''),

    dcc.Graph(
        id='example-graph',
        figure={
            'data': [
                {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
                {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
            ],
            'layout': {
                'title': 'Dash Data Visualization'
            }
        }
    )
])

port = 9990

import os
cocalc_project_id = os.environ['COCALC_PROJECT_ID']

pfx = "/{}/server/{}/".format(cocalc_project_id, port)
app.config.requests_pathname_prefix = pfx

if __name__ == '__main__':
    print("browse to: https://cocalc.com{}".format(pfx))
    app.run_server(debug=True, port=port, host='0.0.0.0')