diff --git a/dynamic_site/site.py b/dynamic_site/site.py index 0efaf25..14c85b1 100644 --- a/dynamic_site/site.py +++ b/dynamic_site/site.py @@ -12,7 +12,7 @@ abort, request, send_from_directory, - Blueprint, + url_for, ) import dynamic_site @@ -35,7 +35,7 @@ class Site: def __init__( self, apps_path: str, - application_root: str = "/", + application_root: str = "", enforce_dev_mode: bool = False, escape_html_in_md: bool = True, verbose: bool = False, @@ -106,13 +106,19 @@ def initialize_flask_server(self) -> None: template_folder=f"{self.dynamic_site_path}/templates", ) - bp = Blueprint("apps", __name__) + def url_for_root(route: str, **kwargs) -> str: + url = url_for(route, **kwargs) + if self.application_root: + url = f"{self.application_root}{url}" + return url - @bp.route("/") + self.flask_app.jinja_env.globals.update(url_for_root=url_for_root) + + @self.flask_app.route("/") def index(): return render_template("index.html", title=self.title, text=self.index_text) - @bp.route("/favicon.ico") + @self.flask_app.route("/favicon.ico") def favicon(): return send_from_directory( os.path.join(self.flask_app.root_path, "static"), @@ -120,14 +126,14 @@ def favicon(): mimetype="image/vnd.microsoft.icon", ) - @bp.route("/assets/") + @self.flask_app.route("/assets/") def assets(file): return send_from_directory( os.path.join(self.flask_app.root_path, "static", "assets"), file, ) - @bp.route("/doc/images/") + @self.flask_app.route("/doc/images/") def doc(file): return send_from_directory( os.path.join(self.flask_app.root_path, "static", "doc", "images"), @@ -138,8 +144,8 @@ def doc(file): # Subdirectory path apps # ----------- - @bp.route("/") - @bp.route("/") + @self.flask_app.route("/") + @self.flask_app.route("/") def app_path_route(app_path): app_name = app_path.replace("/", ".") # First check if the app_path corresponds to an index file @@ -177,7 +183,7 @@ def app_path_route(app_path): "app.html", title=app_title, json_file=json_file, css_file=css_file ) - @bp.route("//compute", methods=["POST"]) + @self.flask_app.route("//compute", methods=["POST"]) def app_path_compute(app_path): app_name = app_path.replace("/", ".") # If the app_name doesn't exist raise a 404 @@ -204,7 +210,7 @@ def app_path_compute(app_path): plots = [stage.serialize() for stage in plots] return jsonify({"docs": docs, "settings": settings, "plots": plots}) - @bp.route("//documentation", methods=["GET"]) + @self.flask_app.route("//documentation", methods=["GET"]) def app_path_documentation(app_path): app_name = app_path.replace("/", ".") # If the app_name doesn't exist raise a 404 @@ -218,9 +224,6 @@ def app_path_documentation(app_path): documentation = open(f"{self.apps_path}/{app_path}.md").read() return jsonify({"text": documentation}) - self.flask_app.register_blueprint(bp) - self.flask_app.config["APPLICATION_ROOT"] = self.application_root - def wsgi(self) -> Flask: return self.flask_app diff --git a/dynamic_site/templates/app.html b/dynamic_site/templates/app.html index 11a5a64..efbd705 100644 --- a/dynamic_site/templates/app.html +++ b/dynamic_site/templates/app.html @@ -6,7 +6,7 @@ @@ -38,10 +38,10 @@ {% else %} - + {% endif %} diff --git a/dynamic_site/templates/index.html b/dynamic_site/templates/index.html index 8025167..11b38c8 100644 --- a/dynamic_site/templates/index.html +++ b/dynamic_site/templates/index.html @@ -6,7 +6,7 @@ @@ -94,7 +94,7 @@