Skip to content

Commit

Permalink
Add dyn serve
Browse files Browse the repository at this point in the history
  • Loading branch information
agahkarakuzu committed Sep 8, 2024
1 parent 33536a5 commit b26499c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
8 changes: 7 additions & 1 deletion api/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import glob
import time
import git
import requests
import json
from flask import abort
from itertools import chain
import yaml
Expand Down Expand Up @@ -388,4 +390,8 @@ def get_directory_content_summary(path):
size = os.path.getsize(file_path)
total_size += size
content.append((file_path.replace(path, '').lstrip('/'), humanize.naturalsize(size)))
return content, humanize.naturalsize(total_size)
return content, humanize.naturalsize(total_size)

def load_json(file_path):
with open(file_path, 'r') as f:
return json.load(f)
23 changes: 23 additions & 0 deletions api/myst_page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ frontend_data.content.title }}</title>
<link rel="stylesheet" href="{{ url_for('serve_theme', filename='styles.css') }}">
<script src="{{ url_for('serve_theme', filename='index.js') }}"></script>
</head>
<body>
<div id="myst-article-root"></div>
<script>
const frontendData = {{ frontend_data | safe }};
window.addEventListener('DOMContentLoaded', (event) => {
if (window.renderMystContent) {
window.renderMystContent(document.getElementById('myst-article-root'), frontendData);
} else {
console.error('renderMystContent function not found. Make sure the theme JS is loaded correctly.');
}
});
</script>
</body>
</html>
54 changes: 52 additions & 2 deletions api/neurolibre_preview_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from flask import jsonify, make_response
from flask import jsonify, make_response, render_template, abort, send_from_directory
from common import *
from schema import BuildSchema, BuildTestSchema, DownloadSchema, MystBuildSchema
from flask_htpasswd import HtPasswdAuth
Expand Down Expand Up @@ -221,4 +221,54 @@ def api_myst_build(user, id, repository_url, commit_hash=None, binder_hash=None)
response = screening.start_celery_task(preview_build_myst_task)
return response

docs.register(api_myst_build)
docs.register(api_myst_build)

# myst_schema = load_myst_schema()
def get_user_build_dir(username,repo,commit):
return os.path.join(username,repo,commit,'_build','site')

def get_theme_dir(username,repo,commit,type):
return os.path.join(username,repo,commit,'_build','templates','site','myst',type)

@app.route('/myst/<username>/<repo>/<commit>/')
@app.route('/myst/<username>/<repo>/<commit>/<path:page_path>')
def render_page(username, repo, commit, page_path=''):
user_build_dir = get_user_build_dir(username, repo, commit)
config_path = os.path.join(user_build_dir, 'config.json')

if not os.path.exists(config_path):
abort(404)

config = load_json(config_path)

if not page_path:
page_path = config['nav'][0]['url']

json_path = os.path.join(user_build_dir, 'content', f"{page_path}.json")
if not os.path.exists(json_path):
abort(404)

page_data = load_json(json_path)

# Prepare data for the frontend
frontend_data = {
'config': config,
'content': page_data,
'base_url': f'/myst/{username}/{repo}/{commit}',
'static_url': '/theme'
}

return render_template('myst_page.html',
username=username,
repo=repo,
commit=commit,
frontend_data=json.dumps(frontend_data))

@app.route('/myst/<username>/<repo>/<commit>/public/<path:filename>')
def serve_public(username, repo, commit, filename):
user_build_dir = get_user_build_dir(username, repo, commit)
return send_from_directory(os.path.join(user_build_dir, 'public'), filename)

@app.route('/theme/<path:filename>')
def serve_theme(filename,username,repo,commit):
return send_from_directory(os.path.join(get_theme_dir(username,repo,commit), 'build'), filename)

0 comments on commit b26499c

Please sign in to comment.