Skip to content

Commit 931f45e

Browse files
author
Mark Gibbs
committed
Moving to a better and separate channels demo
1 parent 233ba91 commit 931f45e

File tree

5 files changed

+50
-11
lines changed

5 files changed

+50
-11
lines changed

demo/demo/asgi.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import os
2+
import django
3+
from channels.routing import get_default_application
4+
5+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "demo.settings")
6+
django.setup()
7+
application = get_default_application()
8+

demo/demo/consumers.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ def __init__(self, *args, **kwargs):
1010
global ALL_CONSUMERS
1111
ALL_CONSUMERS.append(self)
1212

13+
print("Creating a MessageConsumer")
14+
print(len(ALL_CONSUMERS))
15+
16+
self.callcount = 0
17+
1318
def connect(self):
1419
self.accept()
1520

@@ -20,6 +25,7 @@ def disconnect(self, close_code):
2025
if c != self:
2126
ac.append(c)
2227
ALL_CONSUMERS = ac
28+
return super(MessageConsumer, self).disconnect(close_code)
2329

2430
def send_to_widgets(self, channel_name, label, value):
2531
message = json.dumps({'channel_name':channel_name,
@@ -30,6 +36,16 @@ def send_to_widgets(self, channel_name, label, value):
3036
for c in ALL_CONSUMERS:
3137
c.send(message)
3238

39+
self.callcount += 1
40+
if self.callcount > 10:
41+
import gc
42+
print("Running collection")
43+
gc.collect()
44+
self.callcount = 0
45+
46+
import objgraph
47+
objgraph.show_most_common_types()
48+
3349
def receive(self, text_data):
3450
message = json.loads(text_data)
3551

demo/demo/settings.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,19 @@
128128
STATICFILES_DIRS = [
129129
]
130130

131-
import dash_core_components as dcc
132-
_rname = os.path.join(os.path.dirname(dcc.__file__),'..')
133-
for dash_module_name in ['dash_core_components',
134-
'dash_html_components',
135-
'dash_renderer',]:
136-
STATICFILES_DIRS.append( ("dash/%s"%dash_module_name, os.path.join(_rname,dash_module_name)) )
137-
138-
# Fudge to work with channels in debug mode
139-
STATICFILES_DIRS.append(("dash/dpd_components","/home/mark/local/dpd-components/lib"))
131+
# In order to serve dash components locally - not recommended in general, but
132+
# can be useful for development especially if offline - we add in the root directory
133+
# of each module. This is a bit of fudge and only needed if serve_locally=True is
134+
# set on a DjangoDash instance.
135+
136+
if DEBUG:
137+
138+
import dash_core_components as dcc
139+
_rname = os.path.join(os.path.dirname(dcc.__file__),'..')
140+
141+
for dash_module_name in ['dash_core_components',
142+
'dash_html_components',
143+
'dash_renderer',
144+
'dpd_components',]:
145+
STATICFILES_DIRS.append( ("dash/%s"%dash_module_name, os.path.join(_rname,dash_module_name)) )
146+

demo/demo/templates/second_page.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
</div>
1414
<div>
1515
WS Content here
16-
{%plotly_app name="Connected"%}
16+
{%plotly_app slug="connected-2"%}
1717
</div>
1818
<div>
1919
WS Content here
20-
{%plotly_app slug="connected-2"%}
20+
{%plotly_app slug="simpleexample-1"%}
2121
</div>
2222
</body>
23+
{ % plotly_message_pipe % }
2324
</html>

demo/demo/urls.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,10 @@
2727
path('admin/', admin.site.urls),
2828
path('django_plotly_dash/', include('django_plotly_dash.urls')),
2929
]
30+
31+
# Add in static routes so daphne can serve files; these should be masked eg with nginx for production use
32+
33+
from django.conf import settings
34+
from django.conf.urls.static import static
35+
36+
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

0 commit comments

Comments
 (0)