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

Commit

Permalink
Add utility function show_elfinder (#104)
Browse files Browse the repository at this point in the history
* upgrade imjoy-rpc; add show_elfinder

* move to show_elfinder to utils

* Update utils.py
  • Loading branch information
oeway authored Dec 13, 2020
1 parent ba10d0d commit 9140514
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
2 changes: 1 addition & 1 deletion imjoy/VERSION
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "0.10.9",
"version": "0.10.10",
"api_version": "0.2.0"
}
4 changes: 2 additions & 2 deletions imjoy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import json
import os

from imjoy_rpc import api

# read version information from file
IMJOY_PACKAGE_DIR = os.path.dirname(__file__)
with open(os.path.join(IMJOY_PACKAGE_DIR, "VERSION"), "r") as f:
VERSION_INFO = json.load(f)
__version__ = VERSION_INFO["version"]
API_VERSION = VERSION_INFO["api_version"]

from imjoy_rpc import api
62 changes: 61 additions & 1 deletion imjoy/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Provide utilities that should not be aware of ImJoy engine."""
import copy
from importlib import import_module
import json
import os
import string
import sys
import threading
import time
import uuid
from importlib import import_module

if sys.platform == "win32":
from ctypes import windll
Expand All @@ -20,6 +23,63 @@ def get_drives():
return drives


_server_thread = None


def _show_elfinder_colab(root_dir="/content", port=8765, height=600, width="100%"):
from google.colab import output
from imjoy_elfinder.app import main

global _server_thread
if _server_thread is None:

def start_elfinder():
global _server_thread
try:
main(["--root-dir={}".format(root_dir), "--port={}".format(port)])
except OSError:
print("ImJoy-elFinder server already started.")
_server_thread = thread

# start imjoy-elfinder server
thread = threading.Thread(target=start_elfinder)
thread.start()

time.sleep(1)
output.serve_kernel_port_as_iframe(port, height=str(height), width=str(width))


def _show_elfinder_jupyter(url="/elfinder", height=600, width="100%"):
from IPython import display

code = """(async (url, width, height, element) => {
element.appendChild(document.createTextNode(''));
const iframe = document.createElement('iframe');
iframe.src = url;
iframe.height = height;
iframe.width = width;
iframe.style.border = 0;
element.appendChild(iframe);
})""" + "({url}, {width}, {height}, element[0])".format(
url=json.dumps(url), width=json.dumps(width), height=json.dumps(height)
)
display.display(display.Javascript(code))


def show_elfinder(**kwargs):
try:
from google.colab import output

is_colab = True
except ImportError:
is_colab = False

if is_colab:
_show_elfinder_colab(**kwargs)
else:
_show_elfinder_jupyter(**kwargs)


def read_or_generate_token(token_path=None):
token_path = token_path or os.path.join(os.path.expanduser("~"), ".jupyter_token")
# read token from file if exists
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

REQUIREMENTS = [
"numpy",
"imjoy-rpc>=0.2.23",
"imjoy-rpc>=0.2.30",
'pathlib;python_version<"3.4"',
"imjoy-elfinder",
]
except:
REQUIREMENTS = [
"numpy",
"imjoy-rpc>=0.2.23",
"imjoy-rpc>=0.2.30",
'pathlib;python_version<"3.4"',
"jupyter>=1.0.0",
"imjoy-elfinder[jupyter]",
Expand Down

0 comments on commit 9140514

Please sign in to comment.