Skip to content

Commit

Permalink
Possibility to inject subroute into absolute path generation
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelKrispin committed Jul 19, 2023
1 parent fcf4238 commit 0a80fc7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
31 changes: 17 additions & 14 deletions dynamic_site/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
abort,
request,
send_from_directory,
Blueprint,
url_for,
)

import dynamic_site
Expand All @@ -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,
Expand Down Expand Up @@ -106,28 +106,34 @@ 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"),
"favicon.ico",
mimetype="image/vnd.microsoft.icon",
)

@bp.route("/assets/<file>")
@self.flask_app.route("/assets/<file>")
def assets(file):
return send_from_directory(
os.path.join(self.flask_app.root_path, "static", "assets"),
file,
)

@bp.route("/doc/images/<file>")
@self.flask_app.route("/doc/images/<file>")
def doc(file):
return send_from_directory(
os.path.join(self.flask_app.root_path, "static", "doc", "images"),
Expand All @@ -138,8 +144,8 @@ def doc(file):
# Subdirectory path apps
# -----------

@bp.route("/<app_path>")
@bp.route("/<path:app_path>")
@self.flask_app.route("/<app_path>")
@self.flask_app.route("/<path:app_path>")
def app_path_route(app_path):
app_name = app_path.replace("/", ".")
# First check if the app_path corresponds to an index file
Expand Down Expand Up @@ -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("/<path:app_path>/compute", methods=["POST"])
@self.flask_app.route("/<path:app_path>/compute", methods=["POST"])
def app_path_compute(app_path):
app_name = app_path.replace("/", ".")
# If the app_name doesn't exist raise a 404
Expand All @@ -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("/<path:app_path>/documentation", methods=["GET"])
@self.flask_app.route("/<path:app_path>/documentation", methods=["GET"])
def app_path_documentation(app_path):
app_name = app_path.replace("/", ".")
# If the app_name doesn't exist raise a 404
Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions dynamic_site/templates/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link
rel="icon"
type="image/svg+xml"
href="{{ url_for('apps.favicon') }}"
href="{{ url_for_root('favicon') }}"
/>
<meta name="description" content="description" />
<meta name="author" content="author" />
Expand Down Expand Up @@ -38,10 +38,10 @@
<script type="module" src="http://localhost:5173/@vite/client"></script>
<script type="module" src="http://localhost:5173/src/main.tsx"></script>
{% else %}
<link rel="stylesheet" href="{{ url_for('apps.assets', file=css_file) }}" />
<link rel="stylesheet" href="{{ url_for_root('assets', file=css_file) }}" />
<script
type="module"
src="{{ url_for('apps.assets', file=json_file) }}"
src="{{ url_for_root('assets', file=json_file) }}"
></script>
{% endif %}

Expand Down
4 changes: 2 additions & 2 deletions dynamic_site/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link
rel="icon"
type="image/svg+xml"
href="{{ url_for('apps.favicon') }}"
href="{{ url_for_root('favicon') }}"
/>
<meta name="description" content="description" />
<meta name="author" content="author" />
Expand Down Expand Up @@ -94,7 +94,7 @@ <h1 class="uk-heading uk-heading-line uk-text-center">
</div>
<!-- Home Button -->
<div style="position: absolute; left: 5px; top: 5px">
<a href="{{ url_for('apps.index') }}">
<a href="{{ url_for_root('index') }}">
<span className="ukmargin-small-right" data-uk-icon="home"></span>
</a>
</div>
Expand Down

0 comments on commit 0a80fc7

Please sign in to comment.