Skip to content

Instantly share code, notes, and snippets.

@MarcSkovMadsen
Last active November 1, 2023 04:46
Show Gist options
  • Save MarcSkovMadsen/5ccea41b801a8b9f3c555a5b2968586d to your computer and use it in GitHub Desktop.
Save MarcSkovMadsen/5ccea41b801a8b9f3c555a5b2968586d to your computer and use it in GitHub Desktop.
Panel with Tranquilizer REST API
import ast
import requests
url = 'http://localhost:5006/rest/post_data'
def post(value):
data = {'data': value}
response = requests.post(url, data = data)
if response.status_code==200:
print(ast.literal_eval(response.text))
else:
print(response.status_code)
post("Hello")
post("Panel")
post("World")
post("")
panel
requests
tranquilizer
"""Panel provides functionality for integration with REST API providers. Currently Param and
Tranquilizer.
When you `panel serve` the `script.py` you need to add `--rest-provider param` or
`--rest-provider tranquilizer` to serve the REST API.
For more check out the [REST APIs Introduction](https://blog.holoviz.org/panel_0.10.0.html#REST-APIs)
"""
import param
import panel as pn
import traceback as tb
from tranquilizer import tranquilize
pn.extension(sizing_mode="stretch_width")
class ExternalDataHandler(param.Parameterized):
data = param.String(default="")
if not "data_handler" in pn.state.cache:
# This section is run once the first time the page is loaded
data_handler = pn.state.cache["data_handler"] = ExternalDataHandler()
else:
data_handler = pn.state.cache["data_handler"]
@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__))
@tranquilize(method="get")
def data():
try:
return {"data": data_handler.data}
except Exception as e:
return ''.join(tb.format_exception(None, e, e.__traceback__))
text = pn.widgets.StaticText(value=str(data_handler.data))
@pn.depends(data=pn.state.cache["data_handler"].param.data, watch=True)
def on_data_received(data):
text.value = str({"data": data})
pn.template.FastListTemplate(
site="Awesome Panel", title="Panel with REST API", main=[__doc__, text],
).servable()
@MarcSkovMadsen
Copy link
Author

Panel App with REST API

Panel provides functionality for integration with REST API providers. Currently Param and
Tranquilizer.

For more check out the REST APIs Introduction

Credits

Installation

Create a virtual environment and run run pip install panel requests tranquilizer.

Run

To serve the app run panel serve app.py --rest-provider tranquilizer --autoreload.

Then run the code from messages.py in a python terminal or jupyter notebook.

Video

panel_with_rest_api.mp4

Speed up Video and GIF

panel_with_rest_api_speedup.mp4

panel-with-rest-api-speedup

@hhlim333
Copy link

hhlim333 commented Nov 1, 2023

I have followed your approach to do the task.

"http://localhost:5006/rest/data"

and

"http://localhost:5006/rest/post_data"

It works perfectly.

However, I put my panel apps to nginx proxy .

I have to set --prefix="rest" in panel in order to run into nginx proxy successfully.

However, when I set --prefix="rest" in panel . Then I cannot get the data from GET and POST method.

"http://mydomainname/rest/rest/post_data"

and

"http://mydomainname/rest/rest/data"

Do you have any method to solve this problem?

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment