Can a callback have no input?

I’m using the hidden DIV method of sharing state across my Dash app. Sometimes when I’m building a new feature, I don’t want to ‘process’ the data at all. But if I try to use a callback without an input:

@app.callback(
       Output('host-area-data', 'children')
   )
   def data_to_div():
       return dataframe.to_json(date_format='iso', orient='split')

The function isn’t called, so no data is written to the hidden DIV. I end up working around this by inserting
html.Div(id='none',children=[],style={'display': 'none'}), in my layout, and then making the function:

  @app.callback(
        Output('host-area-data', 'children'),
        [Input('none', 'children')]
    )
    def data_to_div(none):
        return dataframe.to_json(date_format='iso', orient='split')

Is there a better way to do this? Like using an event of the page loading to fire the callback?

2 Likes

I do not get why you want to have this coupled to a callback? Wouldn’t you desire by putting your data_to_div content directly in the hidden Div?
Something like:

html.Div('with lots of fancy stuff...', style={'fabulous': 'True'}),
html.Div(dataframe.to_json(date_format='iso', orient='split'), style={'display': 'none'})

I can’t say if this is the case for grahamalama, but as this is something I also find myself doing on occasion when blocking out a dashboard I’ll explain my logic:

When I need to visualise elements from the off, having them feeding off an element that currently has no input but will once the dashboard is complete, then my preferred workflow is to build a callback with no meaningful input in the first place so that when the input is ‘switched on’, it’s a trivial task to update the callback (as opposed to building it in a div, then having to change the structure of my code and transplant it into a callback once the input is live).

In these situations I too use the same solution as grahamalama, so if there is a more elegant way of achieving the same thing I would also be interested to know about it!

1 Like

Yep, Wiper, this is exactly what I was getting at. Thanks for explaining it clearly!