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

**URL:** <https://community.plotly.com/t/is-there-a-way-to-catch-that-someone-refreshed-page/42288>\
**Category:** Dash Python\
**Created:** [July 10, 2020, 11:19am UTC](https://community.plotly.com/t/is-there-a-way-to-catch-that-someone-refreshed-page/42288 "2020-07-10T11:19:31Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrykoz](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@patrykoz](https://community.plotly.com/u/patrykoz)\
**Post date:** [July 10, 2020, 11:19am UTC](https://community.plotly.com/t/is-there-a-way-to-catch-that-someone-refreshed-page/42288/1 "2020-07-10T11:19:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fohrloop](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/fohrloop/32/24888_2.png) [@fohrloop](https://community.plotly.com/u/fohrloop)\
**Post date:** [July 10, 2020, 1:12pm UTC](https://community.plotly.com/t/is-there-a-way-to-catch-that-someone-refreshed-page/42288/2 "2020-07-10T13:12:52Z")

</div>

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

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

```

use

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

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

```

---

<div class="post-metadata">

**Author:** ![patrykoz](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@patrykoz](https://community.plotly.com/u/patrykoz)\
**Post date:** [July 13, 2020, 6:34pm UTC](https://community.plotly.com/t/is-there-a-way-to-catch-that-someone-refreshed-page/42288/4 "2020-07-13T18:34:02Z")

</div>

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() ☹  
Idea looks very well, so how to invoke my method after layout is assigned to app.layout?

---

<div class="post-metadata">

**Author:** ![fohrloop](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/fohrloop/32/24888_2.png) [@fohrloop](https://community.plotly.com/u/fohrloop)\
**Post date:** [July 13, 2020, 6:56pm UTC](https://community.plotly.com/t/is-there-a-way-to-catch-that-someone-refreshed-page/42288/5 "2020-07-13T18:56:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![patrykoz](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@patrykoz](https://community.plotly.com/u/patrykoz)\
**Post date:** [July 13, 2020, 7:02pm UTC](https://community.plotly.com/t/is-there-a-way-to-catch-that-someone-refreshed-page/42288/6 "2020-07-13T19:02:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fohrloop](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/fohrloop/32/24888_2.png) [@fohrloop](https://community.plotly.com/u/fohrloop)\
**Post date:** [July 13, 2020, 7:10pm UTC](https://community.plotly.com/t/is-there-a-way-to-catch-that-someone-refreshed-page/42288/7 "2020-07-13T19:10:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![patrykoz](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@patrykoz](https://community.plotly.com/u/patrykoz)\
**Post date:** [July 13, 2020, 7:15pm UTC](https://community.plotly.com/t/is-there-a-way-to-catch-that-someone-refreshed-page/42288/8 "2020-07-13T19:15:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fohrloop](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/fohrloop/32/24888_2.png) [@fohrloop](https://community.plotly.com/u/fohrloop)\
**Post date:** [July 13, 2020, 8:18pm UTC](https://community.plotly.com/t/is-there-a-way-to-catch-that-someone-refreshed-page/42288/9 "2020-07-13T20:18:08Z")

</div>

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>`

?

---

<div class="post-metadata">

**Author:** ![patrykoz](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@patrykoz](https://community.plotly.com/u/patrykoz)\
**Post date:** [July 15, 2020, 7:56pm UTC](https://community.plotly.com/t/is-there-a-way-to-catch-that-someone-refreshed-page/42288/10 "2020-07-15T19:56:43Z")

</div>

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 😕

---

<div class="post-metadata">

**Author:** ![fohrloop](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/fohrloop/32/24888_2.png) [@fohrloop](https://community.plotly.com/u/fohrloop)\
**Post date:** [July 16, 2020, 2:30pm UTC](https://community.plotly.com/t/is-there-a-way-to-catch-that-someone-refreshed-page/42288/11 "2020-07-16T14:30:30Z")

</div>

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

```python
app.layout = get_layout()

```

it should be

```python
app.layout = get_layout

```

---

<div class="post-metadata">

**Author:** ![patrykoz](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@patrykoz](https://community.plotly.com/u/patrykoz)\
**Post date:** [July 16, 2020, 8:21pm UTC](https://community.plotly.com/t/is-there-a-way-to-catch-that-someone-refreshed-page/42288/12 "2020-07-16T20:21:50Z")

</div>

Thanks a lot! It works!
