Live update by pushing from server (rather than polling or hitting reload)?

I am using a Python shell to interactively run simulations and write data to a file. I have a dash app which renders a bunch of interactive plots from this data. I’d like the dash app to show new data after each run.

Dash’s Live Updating Components page gives two options:

  1. Create a dcc.Interval on the client and have it poll for changed data
  2. Set app.layout = serve_layout to force a page reload to serve a new dynamic layout

The first seems a bit loose (but I guess the check for new data could be kept light) and the second requires the user to hit reload.

So, is there a way for my python process to tell the dash server to trigger a reload? Or some other approach I’ve missed?

2 Likes

I posted this in response to a similar question:

hot-reloading does this for development environments. Since you are saving files, I’m not sure if the hot reloader will pick up on the changes to the file unless you place them in your assets folder 📣 Announcing Hot reload.

Thanks @delsim, this is probably overkill for my current problem. However I’m interested in the architecture for a longer term problem. Is there a design pattern that captures this technique, or is the approach in wider use by web developers do you know?

Thanks @chriddyp, this is a great solution for my current problem and seems to work well.

For anyone reading this, I created an assets folder next to my app.py and literally just touch an empty file in here to trigger a reload:

from pathlib import Path
Path('path/to/dash/app/assets/touch_me_to_cause_dash_reload_while_in_dev_mode').touch()

Then app.py requires app.layout = serve_layout to force a page reload to serve a new dynamic layout.

3 Likes

See also these threads:

I’ve been using the socket approach and finding it quite useful. I’ll post the latest version for those interested.

Hi All,

I am new to Django and Dash and having trouble passing received POST request data to a DjangoDash app instance (in django-plotly-dash). The data being sent in the POST request is originating from another host, but I have CORS headers worked out, and the data is actually turning up in the views method (outlined below).

In “settings.py”, the PLOTLY_DASH value for ‘cache_arguments’ is set to True.

In “views.py”, I have a function which receives requests and returns a rendered template response. If the request method is POST, it gets the json data that was posted, dumps it to a string, and then puts that data string into the context dict…

@csrf_exempt
def session_state_view(request, template_name, **kwargs):

	context = {}
	for k,v in request.session.items():
		context[k]=v
	
	if request.method == 'POST':
		data_str = json.dumps(json.loads(request.body)["data"], ensure_ascii=False)
		context['posted_data'] = data_str
		context['django_plotly_dash']['posted_data'] = data_str
		
	return TemplateResponse(request, template_name, context)

In the app, there is an expanded_callback where I try to get ‘posted_data’ out of the kwargs ‘session_state’ with ‘django_plotly_dash’…

@app.expanded_callback(
    Output('posted_data', 'children'),
    [Input('some_number', 'children')]
)
def update_posted_data(some_number, **kwargs):
    sess_state = kwargs.get('session_state',dict())
    dpd_state = sess_state.get('django_plotly_dash', dict())
    posted_json_str = dpd_state.get('posted_data', None)
    return posted_json_str

Then ‘posted_data’ is returned as the child of an html.Div… except that it isn’t working. ‘posted_json_str’ is always None. What am I missing?

Apologies if this question has been answered numerous times before.

Many thanks in advance,
Sam

Is the post request happening from the same session as the app user? If not then they won’t see the same session_state - this would be the first thing I would check.

You might want to consider an approach where you store/load the information some other way, such as using the Django ORM. If you have a lot of data and/or don’t want to persist it, then the Pipe component might help.

Thanks for your guidance, delsim. The post request session is not happening from the same session as the app user. So, yes, they don’t see the same session state.

I’m trying out your suggestion to use the Django ORM to store/load the data. In using django-plotly-dash, would it make sense to modify the DashApp model by adding another field for the data I’d like to store? Then adjust the view method that handles requests (‘session_state_view’) to to handle the post request by redirecting with the posted data?

Much appreciated,
Sam

Personally, I’d try to avoid modifying any model from an installed package - otherwise, picking up future changes to that package can get nontrivial as the local changes would have to be migrated.

Rather, I’d use some property of the data - there is presumably some app-specific information linking the input to to the app user - and use that as the basis for storing and retrieving data. I don’t know what your app structure is like, but I would look for a relevant Django model to associate the data with; if the updates are too frequent to use the ORM then I would consider using the cache.