-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from delsim/async_use
Prerelease version 0.4.0
- Loading branch information
Showing
17 changed files
with
254 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from channels.generic.websocket import WebsocketConsumer | ||
|
||
import json | ||
|
||
ALL_CONSUMERS = [] | ||
|
||
class MessageConsumer(WebsocketConsumer): | ||
def __init__(self, *args, **kwargs): | ||
super(MessageConsumer, self).__init__(*args, **kwargs) | ||
global ALL_CONSUMERS | ||
ALL_CONSUMERS.append(self) | ||
|
||
def connect(self): | ||
self.accept() | ||
|
||
def disconnect(self, close_code): | ||
ac = [] | ||
global ALL_CONSUMERS | ||
for c in ALL_CONSUMERS: | ||
if c != self: | ||
ac.append(c) | ||
ALL_CONSUMERS = ac | ||
|
||
def send_to_widgets(self, channel_name, label, value): | ||
message = json.dumps({'channel_name':channel_name, | ||
'label':label, | ||
'value':value}) | ||
global ALL_CONSUMERS | ||
|
||
for c in ALL_CONSUMERS: | ||
c.send(message) | ||
|
||
def receive(self, text_data): | ||
message = json.loads(text_data) | ||
|
||
message_type = message.get('type','unknown_type') | ||
|
||
if message_type == 'connection_triplet': | ||
|
||
channel_name = message.get('channel_name',"UNNAMED_CHANNEL") | ||
uid = message.get('uid',"0000-0000") | ||
label = message.get('label','DEFAULT$LABEL') | ||
|
||
# For now, send the uid as value. This essentially 'resets' the value | ||
# each time the periodic connection announcement is made | ||
self.send_to_widgets(channel_name=channel_name, | ||
label=label, | ||
value=uid) | ||
else: | ||
# Not a periodic control message, so do something useful | ||
# For now, this is just pushing to all other consumers indiscrimnately | ||
|
||
channel_name = message.get('channel_name',"UNNAMED_CHANNEL") | ||
uid = message.get('uid',"0000-0000") | ||
value = message.get('value',{'source_uid':uid}) | ||
label = message.get('label','DEFAULT$LABEL') | ||
|
||
self.send_to_widgets(channel_name=channel_name, | ||
label=label, | ||
value=value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from channels.routing import ProtocolTypeRouter, URLRouter | ||
from channels.auth import AuthMiddlewareStack | ||
|
||
from django.conf.urls import url | ||
|
||
from .consumers import MessageConsumer | ||
|
||
application = ProtocolTypeRouter({ | ||
'websocket': AuthMiddlewareStack(URLRouter([url('ws/channel', MessageConsumer),])), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!DOCTYPE HTML> | ||
<html> | ||
<head> | ||
{%load plotly_dash%} | ||
<title>More Examples</title> | ||
</head> | ||
<body> | ||
<div> | ||
<p>Navigational links : | ||
<a href="{%url "home"%}">Main Page</a> | ||
<a href="{%url "second"%}">Second Page</a> | ||
</p> | ||
</div> | ||
<div> | ||
WS Content here | ||
{%plotly_app name="Connected"%} | ||
</div> | ||
<div> | ||
WS Content here | ||
{%plotly_app slug="connected-2"%} | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# | ||
|
||
__version__ = "0.3.0" | ||
__version__ = "0.4.0" | ||
|
||
from .dash_wrapper import DjangoDash | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
django_plotly_dash/templates/django_plotly_dash/plotly_messaging.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{%load staticfiles%} | ||
<script src="{%static "channels/js/websocketbridge.js"%}"></script> | ||
<script> | ||
if( !window.dpd_wsb ) { | ||
const wsb = new channels.WebSocketBridge(); | ||
wsb.connect("{{url}}"); | ||
var dpd_wsb = { | ||
wsb: wsb, | ||
callbacks: [], | ||
add_callback: function(cb) { this.callbacks.push(cb); }, | ||
send:function(message) { this.wsb.send(message); } | ||
}; | ||
wsb.listen(function(action, stream) { | ||
for(var i in dpd_wsb.callbacks) { | ||
dpd_wsb.callbacks[i](action, stream); | ||
} | ||
}); | ||
window.dpd_wsb = dpd_wsb; | ||
if( window.dpd_wsb_pre ) { | ||
for(var i in window.dpd_wsb_pre.callbacks ) dpd_wsb.add_callback(window.dpd_wsb_pre.callbacks[i]); | ||
window.dpd_wsb_pre.callbacks = []; | ||
for(var j in window.dpd_wsb_pre.sender_targets ) window.dpd_wsb_pre.sender_targets[i].add_sender(dpd_wsb); | ||
window.dpd_wsb_pre.sender_targets = []; | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.