-
-
Notifications
You must be signed in to change notification settings - Fork 531
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added FastApi in the user guide for embedding apps * Changed the tutorial and apps to use pn.serve() instead of a thread. Also got rid of the middleware as it is not needed.
- Loading branch information
1 parent
80d1112
commit 04a92a6
Showing
8 changed files
with
37 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,19 @@ | ||
import panel as pn | ||
from bokeh.embed import server_document | ||
from panel.io.server import Server | ||
from fastapi import FastAPI, Request | ||
from fastapi.templating import Jinja2Templates | ||
from starlette.middleware.cors import CORSMiddleware | ||
from starlette.middleware import Middleware | ||
from threading import Thread | ||
from tornado.ioloop import IOLoop | ||
|
||
from sliders.pn_app import createApp | ||
|
||
app = FastAPI() | ||
templates = Jinja2Templates(directory="templates") | ||
|
||
app.add_middleware( # Middleware to serve panel apps asynchronously via starlette | ||
CORSMiddleware, | ||
allow_origins=['*'], | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
) | ||
|
||
|
||
@app.get("/") | ||
async def bkapp_page(request: Request): | ||
script = server_document('http://127.0.0.1:5000/app') | ||
return templates.TemplateResponse("base.html", {"request": request, "script": script}) | ||
|
||
|
||
def bk_worker(): | ||
server = Server({'/app': createApp}, | ||
port=5000, io_loop=IOLoop(), | ||
allow_websocket_origin=["*"]) | ||
|
||
server.start() | ||
server.io_loop.start() | ||
|
||
th = Thread(target=bk_worker) | ||
th.daemon = True | ||
th.start() | ||
pn.serve({'/app': createApp}, | ||
port=5000, allow_websocket_origin=["127.0.0.1:8000"], | ||
address="127.0.0.1", show=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
<title>Panel in FastAPI: sliders</title> | ||
</head> | ||
<body> | ||
<h1>App2</h1> | ||
{{ script|safe }} | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
<title>Panel in FastAPI: sliders</title> | ||
</head> | ||
<body> | ||
<h1>App1</h1> | ||
{{ script|safe }} | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters