Refresh panel app is creating new session

Hi;

First of all, I want to thank all of you. I’m fan of panel.
I’m facing a problem. I built a chat app but when I refresh it, it is creating new session. But this happens when I launch the app with following command.

nohup panel serve --port 4002 chat.py --session-token-expiration 1000 --allow-websocket-origin=ip:4002 > /dev/null 2>&1 &

But if I use the pn.serve(dashboard_object,port=4002) command, it works properly. When I refresh, I can see the previous chat prompts.

I didn’t paste my code because I think if I add some parameter to panel serve, it can work. I need your help to fix my command line expression. If you think it is a bug, let me know. I will share a minimal example.

Thanks in advance.

Likely it creates a new session because you have .show() somewhere in your code.

Hi @ahuang11, I will share an example with you today or tomorrow . There is no .show() in my code. As existing code is very long, it will not be a good example for you. I will try to create small one from my code. Thanks for quick response.

Hi @ahuang11

I tested following code. I see the similar problem with following terminal command.

panel serve --port 8004 replicate_error.py --check-unused-sessions 5000 --unused-session-lifetime 1000

import panel as pn
import param
import time

pn.extension(sizing_mode='stretch_width')

css = """
.bk-panel-models-markup-HTML.markdown.message {
  font-size: 16px; /* Or your desired font size */
}
.bk-input {
    font-size: 16px;
}

.bk-btn.bk-btn-default {
    font-size: 16px;
}
"""

pn.config.raw_css.append(css)


class Chatbot(param.Parameterized):

    pn.chat.ChatMessage(timestamp_format="%b %d, %Y %I:%M %p")

    async def callback(self, contents: str, user: str, instance: pn.chat.ChatInterface):
        instance.placeholder_text = (
                        f"Hang tight! Answer will be ready soon."
                    )
        time.sleep(3)
        message = ""
        for char in "Echoing User: " + contents:
            time.sleep(0.1)
            message += char
            yield message

    def create_layout(self):
        self.chat_interface = pn.chat.ChatInterface(
            callback=self.callback,
            placeholder_text="Waiting for reply...",
            placeholder_threshold=0.1,
            message_params=dict(
                default_avatars={"System": "🔭", "User": "👤"},
                reaction_icons={"like": "thumb-up"},
            ),
            show_rerun=False,
            show_undo=False,
            show_clear=True,
            sizing_mode="stretch_width",
            view_latest=True,
            scroll_button_threshold=2,
            auto_scroll_limit=20000,
        )

        _stylesheets = """
bk-panel-models-markup-HTML markdown message {
  font-size: 6px; /* Or your desired font size */
}
"""

    
        self.chat_interface.send(
            {"object":"Wait for my signal."
             },
            user="System",
            respond=False
        )


        str_pane = pn.pane.Str('Chatbot can make mistakes. Cross-checking answers is recommended.',sizing_mode='stretch_width',
                                styles={'font-size': '12px','color': 'gray', 'text-align': 'center', 'justify-content':'center','margin-top':'20px',
                                        'display':'flex', 'align-items':'center'})
        
        template = pn.template.BootstrapTemplate(
            title="Title",
            header_color = "black",
            sidebar=[],
            main=[pn.Column(self.chat_interface,str_pane)],
            header_background = "black"
        )

        return template
    
dashboard_object = Chatbot().create_layout()
#pn.serve(dashboard_object,port=4002) 
dashboard_object.servable(title="Report")

Hello everyone,

Have you had a chance to review the code. I’m are having a bit of trouble because it is a product in production. Thank you in advance for your support.

I think I misunderstood. Do you mean creating a new session as in, when you refresh, all your chat history is gone? If so, you need to sync it up with user and reload the chat history based on the cache Sync Widgets and URL — Panel v1.3.8
Configuring Basic Authentication — Panel v1.3.8

Thanks. But why is pn.serve(dashboard_object,port=4002) loading existing chatbot history when we refresh. What is the difference between pn.serve and panel serve?