Skip to content

Commit bcafcd4

Browse files
Merge pull request #10 from delsim/async_use
Prerelease version 0.4.0
2 parents 578dcfe + 3205973 commit bcafcd4

File tree

17 files changed

+254
-13
lines changed

17 files changed

+254
-13
lines changed

demo/demo/consumers.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from channels.generic.websocket import WebsocketConsumer
2+
3+
import json
4+
5+
ALL_CONSUMERS = []
6+
7+
class MessageConsumer(WebsocketConsumer):
8+
def __init__(self, *args, **kwargs):
9+
super(MessageConsumer, self).__init__(*args, **kwargs)
10+
global ALL_CONSUMERS
11+
ALL_CONSUMERS.append(self)
12+
13+
def connect(self):
14+
self.accept()
15+
16+
def disconnect(self, close_code):
17+
ac = []
18+
global ALL_CONSUMERS
19+
for c in ALL_CONSUMERS:
20+
if c != self:
21+
ac.append(c)
22+
ALL_CONSUMERS = ac
23+
24+
def send_to_widgets(self, channel_name, label, value):
25+
message = json.dumps({'channel_name':channel_name,
26+
'label':label,
27+
'value':value})
28+
global ALL_CONSUMERS
29+
30+
for c in ALL_CONSUMERS:
31+
c.send(message)
32+
33+
def receive(self, text_data):
34+
message = json.loads(text_data)
35+
36+
message_type = message.get('type','unknown_type')
37+
38+
if message_type == 'connection_triplet':
39+
40+
channel_name = message.get('channel_name',"UNNAMED_CHANNEL")
41+
uid = message.get('uid',"0000-0000")
42+
label = message.get('label','DEFAULT$LABEL')
43+
44+
# For now, send the uid as value. This essentially 'resets' the value
45+
# each time the periodic connection announcement is made
46+
self.send_to_widgets(channel_name=channel_name,
47+
label=label,
48+
value=uid)
49+
else:
50+
# Not a periodic control message, so do something useful
51+
# For now, this is just pushing to all other consumers indiscrimnately
52+
53+
channel_name = message.get('channel_name',"UNNAMED_CHANNEL")
54+
uid = message.get('uid',"0000-0000")
55+
value = message.get('value',{'source_uid':uid})
56+
label = message.get('label','DEFAULT$LABEL')
57+
58+
self.send_to_widgets(channel_name=channel_name,
59+
label=label,
60+
value=value)

demo/demo/plotly_apps.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import dash_core_components as dcc
33
import dash_html_components as html
44

5+
import dpd_components as dpd
6+
57
from django_plotly_dash import DjangoDash
68

79
app = DjangoDash('SimpleExample')
@@ -52,3 +54,33 @@ def callback_c(*args,**kwargs):
5254
da = kwargs['dash_app']
5355
return "Args are [%s] and kwargs are %s" %(",".join(args),str(kwargs))
5456

57+
a3 = DjangoDash("Connected")
58+
59+
a3.layout = html.Div([
60+
dpd.Pipe(id="dynamic",
61+
value="Dynamo 123",
62+
label="rotational energy",
63+
channel_name="test_widget_channel",
64+
uid="need_to_generate_this"),
65+
dpd.Pipe(id="also_dynamic",
66+
value="Alternator 456",
67+
label="momentum",
68+
channel_name="test_widget_channel",
69+
uid="and_this_one"),
70+
dpd.DPDirectComponent(id="direct"),
71+
dcc.RadioItems(id="dropdown-one",options=[{'label':i,'value':j} for i,j in [
72+
("O2","Oxygen"),("N2","Nitrogen"),("CO2","Carbon Dioxide")]
73+
],value="Oxygen"),
74+
html.Div(id="output-three")
75+
])
76+
77+
@a3.expanded_callback(
78+
dash.dependencies.Output('output-three','children'),
79+
[dash.dependencies.Input('dynamic','value'),
80+
dash.dependencies.Input('dynamic','label'),
81+
dash.dependencies.Input('also_dynamic','value'),
82+
dash.dependencies.Input('dropdown-one','value'),
83+
])
84+
def callback_a3(*args, **kwargs):
85+
da = kwargs['dash_app']
86+
return "Args are [%s] and kwargs are %s" %(",".join(args),str(kwargs))

demo/demo/routing.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from channels.routing import ProtocolTypeRouter, URLRouter
2+
from channels.auth import AuthMiddlewareStack
3+
4+
from django.conf.urls import url
5+
6+
from .consumers import MessageConsumer
7+
8+
application = ProtocolTypeRouter({
9+
'websocket': AuthMiddlewareStack(URLRouter([url('ws/channel', MessageConsumer),])),
10+
})

demo/demo/settings.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
'django.contrib.messages',
3939
'django.contrib.staticfiles',
4040

41+
'channels',
42+
4143
'django_plotly_dash.apps.DjangoPlotlyDashConfig',
4244
]
4345

@@ -71,6 +73,7 @@
7173

7274
WSGI_APPLICATION = 'demo.wsgi.application'
7375

76+
ASGI_APPLICATION = 'demo.routing.application'
7477

7578
# Database
7679
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
@@ -127,6 +130,10 @@
127130

128131
import dash_core_components as dcc
129132
_rname = os.path.join(os.path.dirname(dcc.__file__),'..')
130-
for dash_module_name in ['dash_core_components','dash_html_components','dash_renderer',]:
133+
for dash_module_name in ['dash_core_components',
134+
'dash_html_components',
135+
'dash_renderer',]:
131136
STATICFILES_DIRS.append( ("dash/%s"%dash_module_name, os.path.join(_rname,dash_module_name)) )
132137

138+
# Fudge to work with channels in debug mode
139+
STATICFILES_DIRS.append(("dash/dpd_components","/home/mark/local/dpd-components/lib"))

demo/demo/templates/index.html

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
<!DOCTYPE HTML>
22
<html>
3-
{%load plotly_dash%}
4-
<title>Simple stuff</title>
3+
<head>
4+
{%load plotly_dash%}
5+
<title>Simple stuff</title>
6+
</head>
57
<body>
8+
<div>
9+
<p>Navigational links :
10+
<a href="{%url "home"%}">Main Page</a>
11+
<a href="{%url "second"%}">Second Page</a>
12+
</p>
13+
</div>
614
<div>
715
Content here
816
{%plotly_app slug="simpleexample-1" ratio=0.2 %}
@@ -15,5 +23,14 @@
1523
Content here
1624
{%plotly_app name="Ex2"%}
1725
</div>
18-
</body>
26+
<div>
27+
WS Content here
28+
{%plotly_app name="Connected"%}
29+
</div>
30+
<div>
31+
WS Content here
32+
{%plotly_app slug="connected-2"%}
33+
</div>
34+
</body>
35+
{%plotly_message_pipe%}
1936
</html>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
{%load plotly_dash%}
5+
<title>More Examples</title>
6+
</head>
7+
<body>
8+
<div>
9+
<p>Navigational links :
10+
<a href="{%url "home"%}">Main Page</a>
11+
<a href="{%url "second"%}">Second Page</a>
12+
</p>
13+
</div>
14+
<div>
15+
WS Content here
16+
{%plotly_app name="Connected"%}
17+
</div>
18+
<div>
19+
WS Content here
20+
{%plotly_app slug="connected-2"%}
21+
</div>
22+
</body>
23+
</html>

demo/demo/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
import demo.plotly_apps
2323

2424
urlpatterns = [
25-
path('', TemplateView.as_view(template_name='index.html')),
25+
path('', TemplateView.as_view(template_name='index.html'), name="home"),
26+
path('second_page', TemplateView.as_view(template_name='second_page.html'), name="second"),
2627
path('admin/', admin.site.urls),
2728
path('django_plotly_dash/', include('django_plotly_dash.urls')),
2829
]

dev_requirements.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
alabaster==0.7.10
22
argh==0.26.2
3+
asgiref==2.3.2
4+
async-timeout==2.0.1
5+
attrs==18.1.0
6+
autobahn==18.6.1
7+
Automat==0.6.0
38
Babel==2.5.3
49
certifi==2018.4.16
10+
channels==2.1.2
511
chardet==3.0.4
612
click==6.7
13+
constantly==15.1.0
14+
daphne==2.2.0
715
dash==0.21.1
816
dash-core-components==0.22.1
917
dash-html-components==0.10.1
@@ -15,8 +23,10 @@ docutils==0.14
1523
Flask==1.0.2
1624
Flask-Compress==1.4.0
1725
grip==4.5.2
26+
hyperlink==18.0.0
1827
idna==2.6
1928
imagesize==1.0.0
29+
incremental==17.5.0
2030
ipython-genutils==0.2.0
2131
itsdangerous==0.24
2232
Jinja2==2.10
@@ -48,6 +58,9 @@ tornado==5.0.2
4858
tqdm==4.23.3
4959
traitlets==4.3.2
5060
twine==1.11.0
61+
Twisted==18.4.0
62+
txaio==2.10.0
5163
urllib3==1.22
5264
watchdog==0.8.3
5365
Werkzeug==0.14.1
66+
zope.interface==4.5.0

django_plotly_dash/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#
22

3-
__version__ = "0.3.0"
3+
__version__ = "0.4.0"
44

55
from .dash_wrapper import DjangoDash
66

django_plotly_dash/dash_wrapper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def __init__(self, base_pathname=None, replacements = None, ndid=None, expanded_
155155
super(WrappedDash, self).__init__(**kwargs)
156156

157157
self.css.config.serve_locally = True
158-
self.css.config.serve_locally = False
158+
#self.css.config.serve_locally = False
159159

160160
self.scripts.config.serve_locally = self.css.config.serve_locally
161161

0 commit comments

Comments
 (0)