Skip to content

Commit 4187ea9

Browse files
committed
0.2.0rc1
test async handlers in examples
1 parent b005c7e commit 4187ea9

File tree

6 files changed

+35
-18
lines changed

6 files changed

+35
-18
lines changed

bokeh_django/__init__.py

+26-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import inspect
2+
13
# Bokeh imports
24
from bokeh.util.dependencies import import_required
35

@@ -11,17 +13,34 @@
1113
import_required("channels", "The package channels is required by bokeh-django and must be installed")
1214

1315

14-
def with_request(f):
16+
def with_request(handler):
17+
# Note that functools.wraps cannot be used here because Bokeh requires that the signature of the returned function
18+
# must only accept single (Document) argument
1519
def wrapper(doc):
16-
return f(doc, doc.session_context.request)
17-
return wrapper
20+
return handler(doc, doc.session_context.request)
21+
22+
async def async_wrapper(doc):
23+
return await handler(doc, doc.session_context.request)
24+
25+
return async_wrapper if inspect.iscoroutinefunction(handler) else wrapper
26+
27+
28+
def _get_args_kwargs_from_doc(doc):
29+
request = doc.session_context.request
30+
args = request.url_route['args']
31+
kwargs = request.url_route['kwargs']
32+
return args, kwargs
1833

1934

2035
def with_url_args(handler):
36+
# Note that functools.wraps cannot be used here because Bokeh requires that the signature of the returned function
37+
# must only accept single (Document) argument
2138
def wrapper(doc):
22-
request = doc.session_context.request
23-
args = request.url_route['args']
24-
kwargs = request.url_route['kwargs']
39+
args, kwargs = _get_args_kwargs_from_doc(doc)
2540
return handler(doc, *args, **kwargs)
2641

27-
return wrapper
42+
async def async_wrapper(doc):
43+
args, kwargs = _get_args_kwargs_from_doc(doc)
44+
return await handler(doc, *args, **kwargs)
45+
46+
return async_wrapper if inspect.iscoroutinefunction(handler) else wrapper

examples/django_embed/django_embed/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
# Application definition
4141

4242
INSTALLED_APPS = [
43+
'daphne',
4344
'django.contrib.admin',
4445
'django.contrib.auth',
4546
'django.contrib.contenttypes',

examples/django_embed/django_embed/templates/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ <h2>Document Apps</h2>
2121
<ul>
2222
<li><a href="./sea_surface_direct">Sea Surface Temp</a></li>
2323
<li><a href="./sea_surface_with_template">Sea Surface Temp</a> (with custom template rendered by Bokeh)</li>
24-
<li><a href="./sea_surface_bokeh">Sea Surface Temp</a> (loaded using direct path to Bokeh app file - this is effectively the same as the Document app above.)</li>
24+
<li><a href="./sea_surface_bokeh">Sea Surface Temp</a> (loaded using direct path to Bokeh app file - this is effectively the same as the Directory app above.)</li>
2525
<li><a href="./shape_viewer">Shapes (Panel app)</a></li>
2626
</ul>
2727

examples/django_embed/django_embed/views.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ def shape_viewer_handler(doc: Document) -> None:
3030

3131

3232
@with_url_args
33-
def shape_viewer_handler_with_args(doc, arg1, arg2):
33+
async def shape_viewer_handler_with_args(doc, arg1, arg2):
3434
viewer = shape_viewer()
3535
pn.Column(
3636
viewer,
3737
pn.pane.Markdown(f'## This app has URL Args: {arg1} and {arg2}')
3838
).server_doc(doc)
3939

4040

41-
def sea_surface_handler(doc: Document) -> None:
41+
async def sea_surface_handler(doc: Document) -> None:
4242
df = sea_surface_temperature.copy()
4343
source = ColumnDataSource(data=df)
4444

@@ -61,8 +61,8 @@ def callback(attr: str, old: Any, new: Any) -> None:
6161

6262

6363
@with_request
64-
def sea_surface_handler_with_template(doc: Document, request: Any) -> None:
65-
sea_surface_handler(doc)
64+
async def sea_surface_handler_with_template(doc: Document, request: Any) -> None:
65+
await sea_surface_handler(doc)
6666
doc.template = """
6767
{% block title %}Embedding a Bokeh Apps In Django{% endblock %}
6868
{% block preamble %}
@@ -80,7 +80,7 @@ def sea_surface_handler_with_template(doc: Document, request: Any) -> None:
8080
doc.template_variables["username"] = request.user
8181

8282

83-
def sea_surface(request: HttpRequest) -> HttpResponse:
83+
async def sea_surface(request: HttpRequest) -> HttpResponse:
8484
script = server_document(request.get_full_path())
8585
return render(request, "embed.html", dict(script=script))
8686

pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
66

77
[project]
88
name = "bokeh-django"
9-
version = "0.1.0"
9+
version = "0.2.0rc1"
1010
description = "Utility to integrate Bokeh with Django Channels"
1111
readme = "README.md"
1212
authors = [
@@ -22,7 +22,7 @@ keywords = ["Bokeh", "Django", "Channels", "web", "visualization"]
2222
dependencies = [
2323
"bokeh",
2424
"django",
25-
"channels<4",
25+
"channels",
2626
]
2727
requires-python = ">=3.7"
2828

setup.py

-3
This file was deleted.

0 commit comments

Comments
 (0)