Skip to content

Commit

Permalink
Clone and build dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Apr 18, 2023
1 parent 435632e commit 1395a76
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

__pycache__
src/dashboard.json
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"start": "react-scripts start",
"build": "react-scripts build",
"build": "npm run build:dashboard && react-scripts build && npm run copy:dashboard",
"test": "react-scripts test",
"eject": "react-scripts eject"
"eject": "react-scripts eject",
"build:dashboard": "python script/build.py",
"copy:dashboard":"cp -r ./voici_build/voici ./build"
},
"eslintConfig": {
"extends": [
Expand Down
Empty file added script/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions script/build-environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: build-env
channels:
- conda-forge
dependencies:
- python
- pip
- pip:
- voici>=0.3.2,<0.4.0
- jupyterlite-xeus-python>=0.8.0,<0.9.0
69 changes: 63 additions & 6 deletions script/build.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,82 @@
from ast import Dict, List
import json
from pathlib import Path

import subprocess
import sys
from typing import Optional
import yaml
import voici
import logging

from utils import create_dir

logger = logging.getLogger(__file__)
ROOT_DIR = Path(__file__).parents[1]
VOICI_BUILD_DIR = ROOT_DIR / "voici_build"
VOICI_STATIC_DIR = VOICI_BUILD_DIR / "voici"
GIT = "git"
VOICI = "voici"




class Builder:
def __init__(self, config_path: Path) -> None:
self.load_config(config_path)
create_dir(VOICI_BUILD_DIR)
create_dir(VOICI_STATIC_DIR)
self._artifact = []

def load_config(self, config_path: Path) -> None:
with open(config_path, "r") as f:
self._yaml_data: List[Dict] = yaml.load(f, Loader=yaml.CLoader)

def build(self) -> None:
for config in self._yaml_data:
self.build_single(config)
for idx, config in enumerate(self._yaml_data):
self.build_single(idx, config)

artifact_path = ROOT_DIR / 'src' / 'dashboard.json'
with open(artifact_path, 'w') as f:
json.dump(self._artifact, f, indent=2)

def build_single(self, index: int, config: Dict) -> None:
repo_path = VOICI_BUILD_DIR / str(index)
create_dir(repo_path)
cloned = self.clone_repo(config["repo_url"], config.get("branch"), repo_path)
if cloned:
dashboard_url = self.build_voici(repo_path, config.get("content_path", "."), index)
if dashboard_url is not None:
config['dashboard_url'] = dashboard_url
self._artifact.append(config)

def build_single(self, config: Dict) -> None:
pass
def clone_repo(self, url: str, branch: Optional[str], dest: Path) -> bool:
branch = branch or "main"
cmd = ["git", "clone", "-b", branch, url, str(dest)]
logger.debug(f"Cloning {url} at {branch}")
try:
subprocess.run(cmd)
return True
except Exception as e:
logger.error(e)
return False

def build_voici(self, repo_path: Path, content_path: str, index: int) -> Optional[str]:
output_dir = VOICI_STATIC_DIR / str(index)
logger.debug(f"Building voici from {repo_path} to {output_dir}")
cmd = [ "voici", "build", "--contents", content_path, "--output-dir", output_dir]

try:
subprocess.run(cmd, cwd=repo_path)
return f"voici/{index}"
except Exception:
logger.error(e)
return None

if __name__ == "__main__":
config_file = Path(__file__).parent / Path("gallery.yaml")
debug = "--debug" in sys.argv
if debug:
logging.basicConfig(level=logging.DEBUG)

config_file = ROOT_DIR / "script" / Path("gallery.yaml")
builder = Builder(config_file)
builder.build()
10 changes: 9 additions & 1 deletion script/gallery.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
- title: voici-demo
description: First Voici demo
content_path: content/demo.ipynb
build_env: build-environment.yml
repo_url: https://github.com/voila-dashboards/voici-demo
image_url: https://i.imgur.com/IeG75O3.png

- title: voici-demo
description: Second Voici demo
content_path: content
repo_url: https://github.com/voila-dashboards/voici-demo
ref: HEAD
build_env: build-environment.yml
branch: main
image_url: https://i.imgur.com/IeG75O3.png
7 changes: 7 additions & 0 deletions script/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import shutil
from pathlib import Path

def create_dir(path: Path) -> None:
if path.exists():
shutil.rmtree(path)
path.mkdir()
2 changes: 1 addition & 1 deletion src/Gallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function Gallery() {
}}
elevation={4}
>
Link here
<a href="/voici-gallery/voici/0">Link</a>
</Item>
</Grid>
))}
Expand Down

0 comments on commit 1395a76

Please sign in to comment.