From b26499c9ea53a71541d1709a31f3db36a3d567da Mon Sep 17 00:00:00 2001 From: Agah Date: Sun, 8 Sep 2024 12:21:04 -0400 Subject: [PATCH] Add dyn serve --- api/common.py | 8 +++++- api/myst_page.html | 23 +++++++++++++++ api/neurolibre_preview_api.py | 54 +++++++++++++++++++++++++++++++++-- 3 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 api/myst_page.html diff --git a/api/common.py b/api/common.py index 611e389..147d714 100644 --- a/api/common.py +++ b/api/common.py @@ -2,6 +2,8 @@ import glob import time import git +import requests +import json from flask import abort from itertools import chain import yaml @@ -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) \ No newline at end of file + return content, humanize.naturalsize(total_size) + +def load_json(file_path): + with open(file_path, 'r') as f: + return json.load(f) \ No newline at end of file diff --git a/api/myst_page.html b/api/myst_page.html new file mode 100644 index 0000000..3f1efc2 --- /dev/null +++ b/api/myst_page.html @@ -0,0 +1,23 @@ + + + + + + {{ frontend_data.content.title }} + + + + +
+ + + \ No newline at end of file diff --git a/api/neurolibre_preview_api.py b/api/neurolibre_preview_api.py index beb2993..c876cbb 100644 --- a/api/neurolibre_preview_api.py +++ b/api/neurolibre_preview_api.py @@ -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 @@ -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) \ No newline at end of file +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////') +@app.route('/myst////') +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////public/') +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/') +def serve_theme(filename,username,repo,commit): + return send_from_directory(os.path.join(get_theme_dir(username,repo,commit), 'build'), filename) \ No newline at end of file