Skip to content

Commit

Permalink
feat: serve static files from public/
Browse files Browse the repository at this point in the history
  • Loading branch information
nuffin committed Oct 14, 2024
1 parent 9126d8d commit b0b11a9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
32 changes: 22 additions & 10 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

from dash import Dash, html, dcc
import dash_bootstrap_components as dbc
from flask import jsonify, redirect, render_template_string, request, session, url_for
from flask import (
jsonify,
redirect,
render_template_string,
request,
send_from_directory,
session,
url_for,
)
import hashlib
import logging
import plotly.express as px
Expand Down Expand Up @@ -48,15 +56,19 @@
)


@app.route("/")
def home():
return render_template_string(
"""
<h1>Welcome to the Flask + Dash Web App</h1>
<p>This page is served by Flask.</p>
<p>Click here to go to <a href="/dash/">the Dash app</a>.</p>
"""
)
# @app.route("/")
# def home():
# return render_template_string(
# """
# <h1>Welcome to the Flask + Dash Web App</h1>
# <p>This page is served by Flask.</p>
# <p>Click here to go to <a href="/dash/">the Dash app</a>.</p>
# """
# )
@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
def serve_react_app(path):
return send_from_directory(app.static_folder, "index.html")


@app.route("/login", methods=["GET", "POST"])
Expand Down
10 changes: 9 additions & 1 deletion src/common.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from flask import Flask
from flask_cors import CORS

Expand All @@ -9,5 +10,12 @@
if app is None:
server_name = getenv("SERVER_NAME", "Semantic Search Engine")
print(f"Server Name: {server_name}")
app = Flask(server_name)
public_path = os.path.realpath(os.path.join(os.path.dirname(__file__), "public"))
public_path = getenv("PUBLIC_PATH", public_path)
app = Flask(
server_name,
static_folder=public_path,
template_folder=public_path,
static_url_path="/",
)
CORS(app)
7 changes: 3 additions & 4 deletions src/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ def getenv(name, default=None):
global env_loaded
if not env_loaded:
env_name = os.getenv("ENVIRONMENT", "")
env_file = (
f".env.{env_name}" if env_name else ".env"
) # Default to '.env' if not set
load_dotenv(dotenv_path=env_file, verbose=True)
env_file_path = f".env.{env_name}" if env_name else ".env"
print(f'Loading environment variables from "{env_file_path}"')
load_dotenv(dotenv_path=env_file_path, verbose=True)
env_loaded = True
return os.getenv(name, default)

0 comments on commit b0b11a9

Please sign in to comment.