-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1828 from finos/starlette-ws
Add python webserver handlers and clients for starlette (fastapi) and aiohttp
- Loading branch information
Showing
65 changed files
with
2,632 additions
and
604 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
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# python-aiohttp | ||
|
||
This example contains a simple `perspective-python` folder that uses AIOHTTP to serve a static dataset to the user through various [data bindings](https://perspective.finos.org/docs/md/server.html): | ||
|
||
- `index.html`: a [client/server replicated](https://perspective.finos.org/docs/md/server.html#clientserver-replicated) setup that synchronizes the client and server data using Apache Arrow. | ||
- `server_mode.html`: a [server-only](https://perspective.finos.org/docs/md/server.html#server-only) setup that reads data and performs operations directly on the server using commands sent through the Websocket. | ||
- `client_server_editing`: a client-server replicated setup that also enables editing, where edits from multiple clients are applied properly to the server, and then synchronized back to the clients. |
2 changes: 1 addition & 1 deletion
2
examples/tornado-python/package.json → examples/python-aiohttp/package.json
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
################################################################################ | ||
# | ||
# Copyright (c) 2019, the Perspective Authors. | ||
# | ||
# This file is part of the Perspective library, distributed under the terms of | ||
# the Apache License 2.0. The full license can be found in the LICENSE file. | ||
# | ||
import asyncio | ||
import os | ||
import os.path | ||
import logging | ||
import threading | ||
|
||
from aiohttp import web | ||
|
||
from perspective import Table, PerspectiveManager, PerspectiveAIOHTTPHandler | ||
|
||
|
||
here = os.path.abspath(os.path.dirname(__file__)) | ||
file_path = os.path.join( | ||
here, "..", "..", "node_modules", "superstore-arrow", "superstore.arrow" | ||
) | ||
|
||
|
||
def perspective_thread(manager): | ||
"""Perspective application thread starts its own event loop, and | ||
adds the table with the name "data_source_one", which will be used | ||
in the front-end.""" | ||
psp_loop = asyncio.new_event_loop() | ||
manager.set_loop_callback(psp_loop.call_soon_threadsafe) | ||
with open(file_path, mode="rb") as file: | ||
table = Table(file.read(), index="Row ID") | ||
manager.host_table("data_source_one", table) | ||
psp_loop.run_forever() | ||
|
||
|
||
def make_app(): | ||
manager = PerspectiveManager() | ||
|
||
thread = threading.Thread(target=perspective_thread, args=(manager,)) | ||
thread.daemon = True | ||
thread.start() | ||
|
||
async def websocket_handler(request): | ||
handler = PerspectiveAIOHTTPHandler(manager=manager, request=request) | ||
await handler.run() | ||
|
||
app = web.Application() | ||
app.router.add_get("/websocket", websocket_handler) | ||
app.router.add_static( | ||
"/node_modules/@finos", "../../node_modules/@finos", follow_symlinks=True | ||
) | ||
app.router.add_static( | ||
"/node_modules", "../../node_modules/@finos", follow_symlinks=True | ||
) | ||
app.router.add_static("/", "../python-tornado", show_index=True) | ||
return app | ||
|
||
|
||
if __name__ == "__main__": | ||
app = make_app() | ||
logging.critical("Listening on http://localhost:8080") | ||
web.run_app(app, host="0.0.0.0", port=8080) |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# python-starlette | ||
|
||
This example contains a simple `perspective-python` folder that uses Starlette/FastAPI to serve a static dataset to the user through various [data bindings](https://perspective.finos.org/docs/md/server.html): | ||
|
||
- `index.html`: a [client/server replicated](https://perspective.finos.org/docs/md/server.html#clientserver-replicated) setup that synchronizes the client and server data using Apache Arrow. | ||
- `server_mode.html`: a [server-only](https://perspective.finos.org/docs/md/server.html#server-only) setup that reads data and performs operations directly on the server using commands sent through the Websocket. | ||
- `client_server_editing`: a client-server replicated setup that also enables editing, where edits from multiple clients are applied properly to the server, and then synchronized back to the clients. |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "python-starlette", | ||
"private": true, | ||
"version": "1.4.0", | ||
"description": "An example of editing a `perspective-python` server from the browser.", | ||
"scripts": { | ||
"start": "PYTHONPATH=../../python/perspective python3 server.py" | ||
}, | ||
"keywords": [], | ||
"license": "Apache-2.0", | ||
"dependencies": { | ||
"@finos/perspective": "^1.4.0", | ||
"@finos/perspective-viewer": "^1.4.0", | ||
"@finos/perspective-viewer-d3fc": "^1.4.0", | ||
"@finos/perspective-viewer-datagrid": "^1.4.0", | ||
"@finos/perspective-workspace": "^1.4.0", | ||
"superstore-arrow": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"@finos/perspective-webpack-plugin": "^1.4.0", | ||
"npm-run-all": "^4.1.3", | ||
"rimraf": "^2.5.2" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
################################################################################ | ||
# | ||
# Copyright (c) 2019, the Perspective Authors. | ||
# | ||
# This file is part of the Perspective library, distributed under the terms of | ||
# the Apache License 2.0. The full license can be found in the LICENSE file. | ||
# | ||
import asyncio | ||
import os | ||
import os.path | ||
import logging | ||
import threading | ||
import uvicorn | ||
|
||
from fastapi import FastAPI, WebSocket | ||
from fastapi.middleware.cors import CORSMiddleware | ||
from starlette.responses import FileResponse | ||
from starlette.staticfiles import StaticFiles | ||
|
||
from perspective import Table, PerspectiveManager, PerspectiveStarletteHandler | ||
|
||
|
||
here = os.path.abspath(os.path.dirname(__file__)) | ||
file_path = os.path.join( | ||
here, "..", "..", "node_modules", "superstore-arrow", "superstore.arrow" | ||
) | ||
|
||
|
||
def static_nodemodules_handler(rest_of_path): | ||
if rest_of_path.startswith("@finos"): | ||
return FileResponse("../../node_modules/{}".format(rest_of_path)) | ||
return FileResponse("../../node_modules/@finos/{}".format(rest_of_path)) | ||
|
||
|
||
def perspective_thread(manager): | ||
"""Perspective application thread starts its own event loop, and | ||
adds the table with the name "data_source_one", which will be used | ||
in the front-end.""" | ||
psp_loop = asyncio.new_event_loop() | ||
manager.set_loop_callback(psp_loop.call_soon_threadsafe) | ||
with open(file_path, mode="rb") as file: | ||
table = Table(file.read(), index="Row ID") | ||
manager.host_table("data_source_one", table) | ||
psp_loop.run_forever() | ||
|
||
|
||
def make_app(): | ||
manager = PerspectiveManager() | ||
|
||
thread = threading.Thread(target=perspective_thread, args=(manager,)) | ||
thread.daemon = True | ||
thread.start() | ||
|
||
async def websocket_handler(websocket: WebSocket): | ||
handler = PerspectiveStarletteHandler(manager=manager, websocket=websocket) | ||
await handler.run() | ||
|
||
# static_html_files = StaticFiles(directory="../python-tornado", html=True) | ||
static_html_files = StaticFiles(directory="../python-tornado", html=True) | ||
|
||
app = FastAPI() | ||
app.add_api_websocket_route("/websocket", websocket_handler) | ||
app.get("/node_modules/{rest_of_path:path}")(static_nodemodules_handler) | ||
app.mount("/", static_html_files) | ||
|
||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=["*"], | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
) | ||
return app | ||
|
||
|
||
if __name__ == "__main__": | ||
app = make_app() | ||
logging.critical("Listening on http://localhost:8080") | ||
uvicorn.run(app, host="0.0.0.0", port=8080) |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...les/tornado-streaming-python/package.json → ...les/python-tornado-streaming/package.json
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
examples/tornado-python/README.md → examples/python-tornado/README.md
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
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "python-tornado", | ||
"private": true, | ||
"version": "1.4.0", | ||
"description": "An example of editing a `perspective-python` server from the browser.", | ||
"scripts": { | ||
"start": "PYTHONPATH=../../python/perspective python3 server.py" | ||
}, | ||
"keywords": [], | ||
"license": "Apache-2.0", | ||
"dependencies": { | ||
"@finos/perspective": "^1.4.0", | ||
"@finos/perspective-viewer": "^1.4.0", | ||
"@finos/perspective-viewer-d3fc": "^1.4.0", | ||
"@finos/perspective-viewer-datagrid": "^1.4.0", | ||
"@finos/perspective-workspace": "^1.4.0", | ||
"superstore-arrow": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"@finos/perspective-webpack-plugin": "^1.4.0", | ||
"npm-run-all": "^4.1.3", | ||
"rimraf": "^2.5.2" | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
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
Oops, something went wrong.