Is pn.state.cache not set without --autoreload?

I started to use pn.state.cache to store some dataframe and images for my application.
However, I found that if I use only panel serve my_code.py (without --autoreload), pn.state.cache is kept empty. Is it normal behavior?

If it is as expected, it is greatly appreciated if someone could mentions some documentations about it for further reference.

Thanks in advance.

It’s not clear to me what exactly you are after, pn.state.cache is shared globally by all sessions. So I don’t really follow “is kept empty”. It’s definitely not kept empty, e.g. in this trivial app:

import panel as pn

cached = 'key' in pn.state.cache

if 'key' in pn.state.cache:
    value = pn.state.cache['key']
else:
    value = pn.state.cache['key'] = 10

pn.Column(cached, value).servable()

It will display False, 10 the first time and True, 10 on subsequent renders indicating that it looked up the cached value.

1 Like

Hi @philippjfr

I am sorry for unclear question. What I mean by “kept empty” is on the first time, which is resulting
False, 10 by your code. On the other hand, if I use --autoreload, it will show True, 10 since the first time. Probably it is not big deals, however if the users are caching big data, first run could be frustrating, and probably recommendations on using --autoreload while caching should be documented somewhere.

Anyway, thank you for your confirmations. It make me realized some bugs on my code. I learn a lot from this discourse.

1 Like

Ah, I see, thanks for clarifying. You can pass in --warm instead of --autoreload, which will “warm” up the cache but not enable autoloreload.

1 Like