✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

Is there a way to "catch" that someone refreshed page?

As I wrote in topic - is there a way to catch that someone pressed F5? I need it to trigger the method.

If you make your layout to be a callable (function), then it is called every time the layout is created (every time the page is refreshed). So, instead of

layout = html.Div()
app.layout = layout

use

def get_layout():
    layout = html.Div()
    call_some_extra_function()
    return layout

# Note: Not get_layout()
app.layout = get_layout
1 Like

Thanks for response. Unfortunately - it doesn’t work for me. I have to add something to app.layout.children. So - it doesn’t exist during execution of get_layout() :frowning:
Idea looks very well, so how to invoke my method after layout is assigned to app.layout?

What do you mean by having to add something to app.layout.children?

1 Like

So I have app with hardcoded basic layout - few dcc.tabs. User can add new ones,then these are serialized and saved (in json). I do it by app.layout.children+=[newTab] and rest of functions. When I refresh page these tabs vanishing (but they are in json). When I shutdown app and restard these pages are loaded from json and they’re visible in layout.

What is the data type of newTab? Some dash component? Are you doing this inside a callback? Where is the “json” saved? Is the app going to be serving multiple users at the same time?

Just one user, I add dcc.Tab to layout, but I have also a function which returns object of my own class Tab (generally it’s html.div) if you click on one of dcc.Tab from layout. Yes, I do it in callback -json is saved in file in this same callback.

Do I understand this correctly:

  1. App layout is created using app.layout = get_my_layout()
  2. User presses button which is tied to a callback
  3. Callback modifies app.layout.children. Something is added to <div id=mydiv>

Why it could not be

  1. App layout is a function app.layout = get_my_layout
  2. User presses button which is tied to a callback
  3. Callback Output is put to <div id=mydiv>

?

Yeah, I append straight to div instead append to children -and it’s okay. But app.layout = get_my_layout() is not invoked when user refresh a page :confused:

If you’re trying to have the layout to be reloaded every time the page is refreshed, you have to have callable
(function) as app.layout

So, instead of

app.layout = get_layout()

it should be

app.layout = get_layout
2 Likes

Thanks a lot! It works!