CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

| Download

Dash plot visualization from CoCalc project

Views: 1447
Image: ubuntu2004
1
# Demo of Dash in a CoCalc project
2
# Dash tutorial:
3
# https://dash.plot.ly/getting-started
4
# CoCalc HTTP server from a project
5
# https://doc.cocalc.com/howto/webserver.html
6
# Blog articles about Dash with CoCalc
7
# REVISED: https://blog.cocalc.com/cocalc/python/2021/05/03/dash-with-cocalc.html
8
# OUTDATED: https://blog.cocalc.com/cocalc/python/2018/06/08/dash-with-cocalc.html
9
#
10
# 1. Run this file from a .term:
11
# python3 dash-demo.py
12
# 2. When the program runs in the terminal, it will print a URL like this:
13
# https://cocalc.com/<your project id>/server/9990
14
# i.e. if your project id is 30b9a512-6b2c-11e8-a361-4f5344355d2c, the link would be
15
# https://cocalc.com/30b9a512-6b2c-11e8-a361-4f5344355d2c/server/9990
16
# Open a new browser tab to the link that is printed.
17
18
# You must be a collaborator on the project and logged into your CoCalc account
19
# to view the application.
20
21
import dash
22
import dash_core_components as dcc
23
import dash_html_components as html
24
25
import os
26
cocalc_project_id = os.environ['COCALC_PROJECT_ID']
27
port = 9990
28
pfx = "/{}/server/{}/".format(cocalc_project_id, port)
29
app = dash.Dash(requests_pathname_prefix = pfx)
30
31
app.layout = html.Div(children=[
32
html.H1(children='Hello Dash from CoCalc'),
33
34
html.Div(children='''
35
Dash: A web application framework for Python.
36
'''),
37
38
dcc.Graph(
39
id='example-graph',
40
figure={
41
'data': [
42
{'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
43
{'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
44
],
45
'layout': {
46
'title': 'Dash Data Visualization'
47
}
48
}
49
)
50
])
51
52
53
54
if __name__ == '__main__':
55
print("browse to: https://cocalc.com{}".format(pfx))
56
app.run_server(debug=True, port=port, host='0.0.0.0')
57