Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
Include templates and static files (#238)
Browse files Browse the repository at this point in the history
* Include templates and static files

* lazy import

* Fix code

* Disable linter error
  • Loading branch information
oeway authored Oct 15, 2021
1 parent ee85a3b commit 9db2854
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 7 deletions.
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
include imjoy/VERSION
include imjoy/templates/*
include imjoy/startup_apps/*
include imjoy/static_files/*
include LICENSE
2 changes: 1 addition & 1 deletion imjoy/VERSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.11.17"
"version": "0.11.18"
}
24 changes: 19 additions & 5 deletions imjoy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,26 @@
from contextvars import copy_context
from os import environ as env
from typing import Union
from pathlib import Path

import shortuuid
import socketio
import uvicorn
from dotenv import find_dotenv, load_dotenv
from fastapi import FastAPI
from fastapi.logger import logger
from fastapi.staticfiles import StaticFiles
from starlette.requests import Request
from starlette.responses import JSONResponse, PlainTextResponse

from imjoy import __version__ as VERSION
from imjoy.asgi import ASGIGateway
from imjoy.core import EventBus, UserInfo, VisibilityEnum, WorkspaceInfo
from imjoy.core.auth import parse_token
from imjoy.core.connection import BasicConnection
from imjoy.core.interface import CoreInterface
from imjoy.core.plugin import DynamicPlugin
from imjoy.http import HTTPProxy
from imjoy.s3 import S3Controller
from imjoy.apps import ServerAppController
from imjoy.asgi import ASGIGateway


ENV_FILE = find_dotenv()
if ENV_FILE:
Expand Down Expand Up @@ -223,10 +222,19 @@ def create_application(allow_origins) -> FastAPI:
version=VERSION,
)

static_folder = str(Path(__file__).parent / "static_files")
app.mount("/static", StaticFiles(directory=static_folder), name="static")

@app.middleware("http")
async def add_cors_header(request: Request, call_next):
headers = {}
headers["access-control-allow-origin"] = ", ".join(allow_origins)
request_origin = request.headers.get("access-control-allow-origin")
if request_origin and (
allow_origins == "*"
or allow_origins[0] == "*"
or request_origin in allow_origins
):
headers["access-control-allow-origin"] = request_origin
headers["access-control-allow-credentials"] = "true"
headers["access-control-allow-methods"] = ", ".join(["*"])
headers["access-control-allow-headers"] = ", ".join(
Expand Down Expand Up @@ -290,9 +298,15 @@ async def root():
}

if enable_server_apps:
# pylint: disable=import-outside-toplevel
from imjoy.apps import ServerAppController

ServerAppController(core_interface, port=port)

if enable_s3:
# pylint: disable=import-outside-toplevel
from imjoy.s3 import S3Controller

S3Controller(
core_interface.event_bus,
core_interface,
Expand Down
109 changes: 109 additions & 0 deletions imjoy/static_files/lite.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<!DOCTYPE html>
<html lang="en">

<head>
<title>ImJoy-Lite</title>

<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, height=device-height, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes">
<meta name="description" content="ImJoy -- Deep Learning Made Easy!">
<meta name="author" content="Wei OUYANG">
<meta name="keywords" content="Bioimaging, image processing">
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="@weioyang" />
<meta name="twitter:creator" content="@weioyang" />
<meta name="twitter:image" content="https://imjoy.io/static/img/imjoy-card-plain.png" />
<meta property="og:url" content="https://imjoy.io" />
<meta property="og:title" content="ImJoy" />
<meta property="og:description" content="ImJoy -- Deep Learning Made Easy!" />
<meta property="og:image" content="https://imjoy.io/static/img/imjoy-card-plain.png" />
<meta name="format-detection" content="telephone=no">
<link rel="apple-touch-icon" sizes="57x57" href="/static/icons/apple-icon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="/static/icons/apple-icon-60x60.png">
<link rel="apple-touch-icon" sizes="72x72" href="/static/icons/apple-icon-72x72.png">
<link rel="apple-touch-icon" sizes="76x76" href="/static/icons/apple-icon-76x76.png">
<link rel="apple-touch-icon" sizes="114x114" href="/static/icons/apple-icon-114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="/static/icons/apple-icon-120x120.png">
<link rel="apple-touch-icon" sizes="144x144" href="/static/icons/apple-icon-144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="/static/icons/apple-icon-152x152.png">
<link rel="apple-touch-icon" sizes="180x180" href="/static/icons/apple-icon-180x180.png">
<link rel="icon" type="image/png" sizes="192x192" href="/static/icons/android-icon-192x192.png">
<link rel="icon" type="image/png" sizes="32x32" href="/static/icons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="96x96" href="/static/icons/favicon-96x96.png">
<link rel="icon" type="image/png" sizes="16x16" href="/static/icons/favicon-16x16.png">
<link rel="shortcut icon" href="static/icons/favicon.ico" type="image/x-icon" />
<link rel="icon" href="static/icons/favicon.ico" type="image/x-icon" />
<link rel="apple-touch-startup-image" href="/launch.png">
<link rel="manifest" href="manifest.json">
<meta name="msapplication-TileColor" content="#f5f9ff">
<meta name="msapplication-TileImage" content="static/icons/ms-icon-144x144.png">
<meta name="theme-color" content="#f5f9ff">
<script src="https://lib.imjoy.io/imjoy-loader.js"></script>
</head>

<body style="width:100%;height:100vh;">
<div id="menu-container" style="position: absolute; right: 0; top: 4px; z-index: 999"></div>
<div id="window-container" style="height: 100vh"></div>
<script>
loadImJoyBasicApp({
version: "0.14.0",
process_url_query: true,
show_window_title: false,
show_progress_bar: true,
show_empty_window: true,
menu_container: "menu-container",
menu_style: {},
window_style: {
width: "100%",
height: "100%"
},
main_container: null,
window_manager_container: "window-container",
imjoy_api: {}
}).then(async app => {
const api = app.imjoy.api;
app.addMenuItem({
label: "➕ Load Plugin",
callback() {
const uri = prompt(
`Please type a ImJoy plugin URL`,
"https://raw.githubusercontent.com/imjoy-team/imjoy-plugins/master/repository/welcome.imjoy.html"
);
if (uri) {
app.loadPlugin(uri).then((plugin) => {
app.runPlugin(plugin)
})
}
},
});
app.addMenuItem({
label: "🧩 ImJoy Fiddle",
async callback() {
const plugin = await app.loadPlugin("https://if.imjoy.io")
await app.runPlugin(plugin)
app.removeMenuItem("🧩 ImJoy Fiddle")
},
});

});
</script>
<style>
#menu-container {
opacity: 0.4;
}

#menu-container:hover {
opacity: 1.0;
}

.window{
min-height: 200px;
}
</style>
</body>

</html>
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ python-socketio[asyncio_client]==5.0.4 # pin for now to solve unknown connectio
pyyaml==5.4.1
shortuuid==1.0.1
uvicorn==0.15.0
requests==2.26.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
install_requires=REQUIREMENTS,
extras_require={
"jupyter": ["jupyter>=1.0.0", "ipykernel>=5.1.4", "imjoy-jupyter-extension"],
"server-apps": ["playwright>=1.15.0"],
"server-apps": ["requests>=2.26.0", "playwright>=1.15.0"],
},
zip_safe=False,
entry_points={"console_scripts": ["imjoy = imjoy.__main__:main"]},
Expand Down

0 comments on commit 9db2854

Please sign in to comment.