Thushar Mohan
Thushar Mohan

Reputation: 1

template.serveable() not working as expected in Python panel

I have the four CSV files stored in local. Each corresponds to some store details which has ItemNumber, ItemName, ItemBrand, and ItemCost.

I am using a Python panel to display the CSV files in a browser after converting the CSV as an interactive data frame. When I execute the code, the app is launched in browser. But when I change the dropdown value, the contents of the webpage is not updated. The error says:

2023-06-16 23:38:24,500 ERROR: panel.reactive - Callback failed for object named "Select File Name" changing property {'value': 'File2'} . OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted

I have added some logic to stop the template and start it with the new content each time the drop down value is changed, to prevent the simultaneous use of same port number. But somehow , it is not working. Please help me to fix this issue:

    #!/usr/bin/env python3

    import pandas as pd
    from pathlib import Path
    import panel as pn
    import hvplot.pandas

    fileName = "File1"
    df_file_name = fileName + "_df.csv"
    df_filepath = Path('C:\\Users\\user1\\MyPythonApp\\' + df_file_name)
    serveable = None

    def dfToDisplay(file):
        global serveable  # Access the 'serveable' variable

        # Stop the previous 'servable' if it exists
        if serveable is not None:
            serveable.stop()
            time.sleep(10)
        if type(file) == str:
            filenm = file
        else:
            filenm = file[-2]

        df_path = Path("C:\\Users\\user1\\MyPythonApp\\" + filenm + "_df.csv")
        displayDF = pd.read_csv(df_path)
        interactivedf = displayDF.interactive()
        itable = interactivedf.pipe(pn.widgets.Tabulator, pagination='local', page_size=50, layout='fit_data', theme='default', disabled=True, widths={'index': '70', 'ItemNumber': '130', 'ItemName': '200', 'ItemBrand': '150', 'ItemCost': '900'})
        template = pn.template.FastListTemplate(title='Blah blah Details', sidebar=['Filename', dropdown], logo='https://abccorp.com/logo.svg', main=[itable.panel()])
        serveable = pn.serve(template, port=20001, show=False)

    dropdown = pn.widgets.Select(name='Select File Name', options=['File1', 'File2', 'File3', 'File4'])
    dropdown.param.watch(dfToDisplay, 'value')
    dfToDisplay(fileName)

Application screenshot

I tried it using template.show() method. In this case, each time a new server is launched in a random port numbers. But I need the data across different values of dropdown to be displayed on a fixed port number.

Upvotes: 0

Views: 546

Answers (0)

Related Questions