Skip to content

Commit

Permalink
Blockops prefix in deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelKrispin committed Jul 19, 2023
1 parent 9f070a5 commit 98ab576
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
41 changes: 28 additions & 13 deletions dynamic_site/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
from inspect import getmembers, isclass
from typing import Any

from flask import Flask, jsonify, render_template, abort, request, send_from_directory
from flask import (
Flask,
jsonify,
render_template,
abort,
request,
send_from_directory,
Blueprint,
)

import dynamic_site
from dynamic_site.app import App, ResponseError
Expand All @@ -27,11 +35,13 @@ class Site:
def __init__(
self,
apps_path: str,
url_prefix: str = "",
enforce_dev_mode: bool = False,
escape_html_in_md: bool = True,
verbose: bool = False,
) -> None:
self.apps_path = apps_path
self.url_prefix = url_prefix
self.enforce_dev_mode = enforce_dev_mode
self.dynamic_site_path = os.path.dirname(dynamic_site.__file__)
self.render_md = mistune.create_markdown(
Expand All @@ -46,7 +56,7 @@ def __init__(

self.initialize_flask_server()

def make_index_text(self, file: str, folder: str="") -> tuple[str, str]:
def make_index_text(self, file: str, folder: str = "") -> tuple[str, str]:
# Note that this conversion isn't really clean...
if not os.path.exists(file):
raise FileNotFoundError(f"{file} file couldn't be found!")
Expand Down Expand Up @@ -93,30 +103,31 @@ def initialize_flask_server(self) -> None:
self.flask_app = Flask(
__name__,
static_folder=STATIC_FOLDER,
# static_url_path="",
template_folder=f"{self.dynamic_site_path}/templates",
)

@self.flask_app.route("/")
bp = Blueprint("apps", __name__)

@bp.route("/")
def index():
return render_template("index.html", title=self.title, text=self.index_text)

@self.flask_app.route("/favicon.ico")
@bp.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",
)

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

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

@self.flask_app.route("/<app_path>")
@self.flask_app.route("/<path:app_path>")
@bp.route("/<app_path>")
@bp.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
try:
index_file_path = os.path.join(self.apps_path, app_path, "index.md")
title, text = self.make_index_text(index_file_path, folder=app_path+'/')
title, text = self.make_index_text(
index_file_path, folder=app_path + "/"
)
except FileNotFoundError:
pass
else:
Expand Down Expand Up @@ -164,7 +177,7 @@ def app_path_route(app_path):
"app.html", title=app_title, json_file=json_file, css_file=css_file
)

@self.flask_app.route("/<path:app_path>/compute", methods=["POST"])
@bp.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 @@ -191,7 +204,7 @@ def app_path_compute(app_path):
plots = [stage.serialize() for stage in plots]
return jsonify({"docs": docs, "settings": settings, "plots": plots})

@self.flask_app.route("/<path:app_path>/documentation", methods=["GET"])
@bp.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 @@ -205,6 +218,8 @@ 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, url_prefix=self.url_prefix)

def wsgi(self) -> Flask:
return self.flask_app

Expand Down
10 changes: 7 additions & 3 deletions dynamic_site/templates/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{ title }}</title>
<link rel="icon" type="image/svg+xml" href="{{ url_for('favicon') }}" />
<link
rel="icon"
type="image/svg+xml"
href="{{ url_for('apps.favicon') }}"
/>
<meta name="description" content="description" />
<meta name="author" content="author" />
<meta name="keywords" content="keywords" />
Expand Down Expand Up @@ -34,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('assets', file=css_file) }}" />
<link rel="stylesheet" href="{{ url_for('apps.assets', file=css_file) }}" />
<script
type="module"
src="{{ url_for('assets', file=json_file) }}"
src="{{ url_for('apps.assets', file=json_file) }}"
></script>
{% endif %}

Expand Down
10 changes: 7 additions & 3 deletions dynamic_site/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{ title }}</title>
<link rel="icon" type="image/svg+xml" href="{{ url_for('favicon') }}" />
<link
rel="icon"
type="image/svg+xml"
href="{{ url_for('apps.favicon') }}"
/>
<meta name="description" content="description" />
<meta name="author" content="author" />
<meta name="keywords" content="keywords" />
Expand Down Expand Up @@ -90,8 +94,8 @@ <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('index') }}">
<span className="uk-margin-small-right" data-uk-icon="home"></span>
<a href="{{ url_for('apps.index') }}">
<span className="ukmargin-small-right" data-uk-icon="home"></span>
</a>
</div>
</body>
Expand Down
1 change: 1 addition & 0 deletions web.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
def deploy():
site = Site(
apps_path="web_apps",
url_prefix="/blockops",
enforce_dev_mode=False,
escape_html_in_md=False,
)
Expand Down

0 comments on commit 98ab576

Please sign in to comment.