Skip to content

Commit

Permalink
chore: initial pass on trame code
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Jul 24, 2023
1 parent 338787e commit 158f01e
Show file tree
Hide file tree
Showing 7 changed files with 12,110 additions and 31 deletions.
163 changes: 163 additions & 0 deletions example.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "6385f929-aa4c-4f03-83c9-41e3bc95af83",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.environ[\"TRAME_DISABLE_V3_WARNING\"] = \"1\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9edec8b3-a309-4da7-824e-da1443d07b85",
"metadata": {},
"outputs": [],
"source": [
"from trame.app import get_server\n",
"\n",
"server = get_server()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "9b34d663-dcdd-40f0-8966-b6d6578c1932",
"metadata": {},
"outputs": [],
"source": [
"from trame.ui.vuetify import SinglePageLayout\n",
"\n",
"layout = SinglePageLayout(server)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "cdbb028f-4da9-4abe-a198-ecfa555b9e23",
"metadata": {},
"outputs": [],
"source": [
"from trame.widgets import vuetify, html\n",
"\n",
"with layout:\n",
" with layout.toolbar as tb:\n",
" vuetify.VSpacer()\n",
" vuetify.VBtn(\"Hello\")\n",
" with layout.content:\n",
" with vuetify.VContainer(classes=\"fluid fill-height pa-0 ma-0\"):\n",
" html.Div(\"Hello world\", style=\"background: red\")\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c971b380-3145-47ac-b9ca-1ae9dc0649d4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Future pending>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"server.ready"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "883d7c0c-ca78-491b-838c-ce5e445e5736",
"metadata": {},
"outputs": [],
"source": [
"server.start(exec_mode=\"task\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "b275ef23-d3b6-4814-8eb2-7503514c7a8f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Future pending>"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"server.ready"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "56158e6b-a230-436e-8bdb-f03bbba94c5c",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
" The server is not running. Before displaying a trame layout you should await it to make sure it is ready.\n",
" You should run the following code before displaying a layout.\n",
" <pre style=\"padding: 5px; border: solid 1px rgb(224,224,224); background: rgb(245,245,245);\">await layout.ready</pre>\n",
" </div>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"layout"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "397dc56d-5202-4c5c-bce3-3e7952c7d226",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.17"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@
},
"jupyterlab": {
"discovery": {
"server": {
"managers": [
"pip"
],
"base": {
"name": "trame_jupyter_server"
"server": {
"managers": [
"pip"
],
"base": {
"name": "trame_jupyter_server"
}
}
}
},
"extension": true,
"outputDir": "trame_jupyter_server/labextension"
Expand Down
5 changes: 5 additions & 0 deletions style/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
https://jupyterlab.readthedocs.io/en/stable/developer/css.html
*/

.trame-iframe {
border: none;
width: 100%;
}
10 changes: 3 additions & 7 deletions trame_jupyter_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,18 @@
# in editable mode with pip. It is highly recommended to install
# the package from a stable release or in editable mode: https://pip.pypa.io/en/stable/topics/local-project-installs/#editable-installs
import warnings

warnings.warn("Importing 'trame_jupyter_server' outside a proper installation.")
__version__ = "dev"
from .handlers import setup_handlers


def _jupyter_labextension_paths():
return [{
"src": "labextension",
"dest": "trameComm"
}]
return [{"src": "labextension", "dest": "trameComm"}]


def _jupyter_server_extension_points():
return [{
"module": "trame_jupyter_server"
}]
return [{"module": "trame_jupyter_server"}]


def _load_jupyter_server_extension(server_app):
Expand Down
52 changes: 40 additions & 12 deletions trame_jupyter_server/handlers.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
import json
import os
import tempfile
from pathlib import Path

from jupyter_server.base.handlers import APIHandler
from jupyter_server.utils import url_path_join
import tornado

class RouteHandler(APIHandler):
# The following decorator should be present on all verb methods (head, get, post,
# patch, put, delete, options) to ensure only authorized user can request the
# Jupyter server
@tornado.web.authenticated
def get(self):
self.finish(json.dumps({
"data": "This is /trame-jupyter-server/get-example endpoint!"
}))
from trame.tools.www import StaticContentGenerator

# Static directory to serve
TRAME_STATIC_WWW = tempfile.mkdtemp()

# Change default backend for generic
# os.environ["TRAME_BACKEND"] = "generic" # FIXME - Uncomment when ready...


# Generate trame static content
def write_static_www(*modules):
for client_type in ["vue2", "vue3"]:
generator = StaticContentGenerator()
generator.client_type = client_type
generator.enable_all()
if len(modules) and modules[0]:
generator.enable_modules(*modules)
generator.write(Path(TRAME_STATIC_WWW) / client_type)


# Generate static content
write_static_www(*os.environ.get("TRAME_MODULES", "").split(","))


# class RouteHandler(APIHandler):
# # The following decorator should be present on all verb methods (head, get, post,
# # patch, put, delete, options) to ensure only authorized user can request the
# # Jupyter server
# @tornado.web.authenticated
# def get(self):
# self.finish(json.dumps({
# "data": "This is /trame-jupyter-server/get-example endpoint!"
# }))


def setup_handlers(web_app):
host_pattern = ".*$"

base_url = web_app.settings["base_url"]
route_pattern = url_path_join(base_url, "trame-jupyter-server", "get-example")
handlers = [(route_pattern, RouteHandler)]
static = url_path_join(base_url, "trame-jupyter-server", "(.*)")
handlers = [
(static, tornado.web.StaticFileHandler, dict(path=TRAME_STATIC_WWW)),
]
web_app.add_handlers(host_pattern, handlers)
12 changes: 7 additions & 5 deletions trame_jupyter_server/tests/test_handlers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import json
from pathlib import Path

from trame_jupyter_server.handlers import TRAME_STATIC_WWW


async def test_get_example(jp_fetch):
# When
response = await jp_fetch("trame-jupyter-server", "get-example")
response = await jp_fetch("trame-jupyter-server", "vue2", "index.html")

# Then
assert response.code == 200
payload = json.loads(response.body)
assert payload == {
"data": "This is /trame-jupyter-server/get-example endpoint!"
}
txt_content = response.body
source_content = (Path(TRAME_STATIC_WWW) / "vue2" / "index.html").read_text("utf-8")
assert txt_content == source_content

0 comments on commit 158f01e

Please sign in to comment.