Send data to a Panel application

Hello,
I am developing a Panel application which is fed with data by another software
I did a little proof of concept which uses the tranquilize REST API and which allows to POST data from the other application
However I am not sure this is the best solution.
Does anyone encountered this problem?
Or has a better idea to solve it?

Here is the POC:

  • The panel application served with: panel serve .\rest_api_test.ipynb --show --rest- provider tranquilizer
import param
import panel as pn
import traceback as tb
from tranquilizer import tranquilize


class ExternalDataHandler(param.Parameterized):
    data = param.String(default="")

pn.state.cache["data_handler"] = ExternalDataHandler()

@tranquilize(method="post")
def post_data(data):
    try:
        pn.state.cache["data_handler"].data = data
        return f"transaction succeed"
    except Exception as e:
        return ''.join(tb.format_exception(None, e, e.__traceback__))
    
text = pn.widgets.StaticText(value="")
@pn.depends(data=pn.state.cache["data_handler"].param.data, watch=True)
def on_data_received(data):
    text.value = str(data)
app = pn.panel(text)
app.servable()

And the simulation of the other application sending data:

import ast
import requests

url = 'http://localhost:5006/rest/post_data'
data = {'data': 'Test'}

print(ast.literal_eval(requests.post(url, data = data).text))

This is the result:
rest_app_example

1 Like

Thanks for the inspiration @xavArtley. I polished it a bit, made a gist and shared on social media

panel-with-rest-api-speedup

1 Like