Data is updated only when server gets started when panel apps served with pn.serve

I try to serve multiple apps with pn.serve. Apps is in the same folder and they are each wrapped in functions separately. Apps get data from database. The issue here is that they fetch data only when server restarts. When I refresh web browser and when there is new data on db, I cant see the updated data. It is as if app is initialized once at startup and no more updating. Thats the main.py:

import panel as pn
import os
from apps import harita,template1

harita = harita.ornekle()
cihaz1 = template1.create('cihaz1')

ROUTES = {
    "Tum_Projeler": harita, 
    "Vitra_Karo_SprayDryer1": cihaz1
}
pn.serve(ROUTES, port=5006)

and here is the head of the code for template1.py:

import numpy as np
import pandas as pd
from statsmodels.tsa.holtwinters import ExponentialSmoothing
from sklearn.metrics import mean_squared_error
import panel as pn
import influxdb_client, os, time, json
from datetime import datetime, timezone
import folium
import matplotlib.pyplot as plt
from matplotlib.figure import Figure

import warnings
warnings.filterwarnings("ignore")

pn.extension(loading_spinner='dots', loading_color='#00aa41')



def create(cihaz_tag):
    
 
        INFLUXDB_TOKEN="rbQ2hHdTwSIBWZJT4JyvA8MEyuQ0VRkfmXK21CvFCqF1saA1qB6Zaw3WEng_90QaG4fmY4yraxB036ZBdLXkLQ=="

    #token = os.environ.get("INFLUXDB_TOKEN")
    org = "Sankontrol"

    if os.name == 'nt':
        influxdb_url = "http://154.53.166.5:8086"
    else:
        influxdb_url = "http://localhost:8086"

Hi @sansal54

Welcome to the community. The problem is that you run the functions and provide their (fixed) return values to the ROUTES function before you start the Panel server.

Instead you should provide the functions directly to ROUTES, to let the Panel server execute them each time a page is opened.

Try the example below and you should see the difference:

import panel as pn
from datetime import datetime

def harita_ornekle():
    return datetime.now()

def template1_create(key):
    return f"{key} {datetime.now()}"

harita = harita_ornekle()
cihaz1 = template1_create('cihaz1')

ROUTES = {
    "Tum_Projeler": harita, 
    "Vitra_Karo_SprayDryer1": cihaz1,
    "Tum_Projeler_dynamic": harita_ornekle, 
    "Vitra_Karo_SprayDryer1_dynamic": lambda: template1_create('chihaz1'),
}
pn.serve(ROUTES, port=5006)
1 Like

Thanks a lot, it works that way.