I have a Django app that uses Bokeh to show charts. I have multiple apps, and within each app, I have multiple ‘pages’, each page roughly corresponding to a template. One one page, I have a Bokeh chart and a year selection (range) widget.
I want to use the widget to select a year and have Django retrieve the appropriate data from the database and display it. I don’t want to re-render the whole page, just update the Bokeh plot data (ColumnDataSource). I’m at a bit of a loss to know how to do this. Code gen is suggesting I create an API, but this feels like overkill.
Here’s what I want to do:
The user selects a year on the slider
There’s a callback that gets activated, the callback queries Django to get the appropriate year’s data
The callback updates the Bokeh ColumnDataSource to update the plot.
I’m not sure how to make the Django query and how to get the response.
Has anyone done anything like this? How do people update charts via widgets without re-rendering everything?
Don’t read too much into this.
In the general case, a Django view is an API. It receives a web request and returns a response.
What generates that web request is immaterial.
What Django returns as a response can be whatever the requestor requires. It could be HTML, JSON, csv, plain text, whatever.
So yes, what you have can work - but it’s up to you to define what data needs to be sent & received.
Hallo!
I agree with what say above.
You can select - what/which from the response will be return to the page/front.
APi very good whan the your front is doesn’t know about server/back.
Or simple return.
API is often used from django-rest-framework. I (myself) use this is library, all time. It’s very nice for synchronous logic .
Or ASYNC REST framework. IT is copy of ‘django-rest-framework’ just only async version.
And for a simple code version - any a code for view.
Your post it is abstract message. And you wanted would get a full response on the your question (if i understand correctly).
FOr fully answer this, we need to look the existing logic.
I think this.
@KenWhitesell and @Tryd0g0lik : thank you very much for your helpful feedback. @KenWhitesell what you said was 100% correct and extremely helpful. I’m going to mark you as solving it.
Here’s what I did in the end:
- Added a view JSON API that returned JSOn data formatted in exactly the way I wanted it. I had to change a few other functions to make them more generic so I could re-use code.
- Added a callback to my widget that called the JSON API to get data. I then updated the Bokeh chart with the data.
One of the hard parts was finding the Bokeh chart outside of the CustomJS system. I did this using a line of code like this.
model = Bokeh.documents[violinDataIndex].get_model_by_name(modelName)
Once again, thank you for replying. What you said was the key to me fixing this.