Hello!
My use case is fairly simply and common: I want have a bokeh plot (on client browser) be refreshed with data from the server every 1 sec, using an ajax request.
For testing this, I simply draw a circle at 0,0 initially, which shall then be updated to 10,10. The code is as follows.
Python:
from django.shortcuts import render
from django.http import JsonResponse
from bokeh.models import AjaxDataSource
from bokeh.plotting import figure
from bokeh.embed import components
from bokeh.resources import INLINE
def index(request):
plot = figure()
source = AjaxDataSource(data_url='http://localhost:8000/view2d/data/',
polling_interval=1000)
source.data = dict(x=[0], y=[0]) # workaround
plot.circle(source=source, x='x', y='y')
script, div = components(plot, INLINE)
js_resources = INLINE.render_js()
css_resources = INLINE.render_css()
context = {
'bokeh_script' : script,
'bokeh_div' : div,
'js_resources' : js_resources,
'css_resources' : css_resources
}
return render(request, 'view2d/index.html', context)
def data(request):
response = JsonResponse(dict(x=[10],y=[10]))
response[“Access-Control-Allow-Origin”] = “"
response[“Access-Control-Allow-Methods”] = “GET, POST, OPTIONS”
response[“Access-Control-Max-Age”] = “1000”
response[“Access-Control-Allow-Headers”] = "”
return response
``
Html:
{% block content %}
{{bokeh_div|safe}}
{% endblock %}
{% block scripts %}
{{ js_resources|safe }}
{{ css_resources|safe }}
{{ bokeh_script|safe }}
{% endblock %}
``
Unfortunately I do not get an update of the plot (circle stays at 0,0) but I can see the request being issued every second. This is the output:
[19/Jul/2016 16:19:25] “OPTIONS /view2d/data/ HTTP/1.1” 200 22
[19/Jul/2016 16:19:26] “OPTIONS /view2d/data/ HTTP/1.1” 200 22
[19/Jul/2016 16:19:27] “OPTIONS /view2d/data/ HTTP/1.1” 200 22
[19/Jul/2016 16:19:28] “OPTIONS /view2d/data/ HTTP/1.1” 200 22
[19/Jul/2016 16:19:29] “OPTIONS /view2d/data/ HTTP/1.1” 200 22
[19/Jul/2016 16:19:30] “OPTIONS /view2d/data/ HTTP/1.1” 200 22
[19/Jul/2016 16:19:31] “OPTIONS /view2d/data/ HTTP/1.1” 200 22
``
I am quite sure this problem has to do with cross-domain request restriction. Does anyone has a simply solution to this? I saw examples for flask (e.g. here: http://flask.pocoo.org/snippets/56/) but want to avoid decorators or middleware solutions.