How can I write a path to a file that is in my project root directory inside my html file that's in templates?
My index.html file is located at root/ui/webapp/public/templates/index.html and I'm trying to use a script tag to bundle a webpacked javascript file located at root/dist/bundle.js.
I'm using python and flask as my web server, and I have a filepath for webapp as above as well as an identical path for mobileapp. When I navigate to the page I'm checking the useragent to direct them to the correct version of index.html. See below:
from flask import Flask, render_template, request
from user_agents import parse
mobile_templates='ui/mobileapp/public/templates/'
web_templates='ui/webapp/public/templates/'
mobile_static='ui/mobileapp/public/static/'
web_static='ui/webapp/public/static/'
app = Flask(__name__, template_folder=web_templates, static_folder=web_static)
app.register_blueprint(account_manager)
def check_useragent():
user_agent = request.headers.get('User-Agent')
ua = parse(user_agent)
if ua.is_mobile:
app.template_folder = mobile_templates
app.static_folder = mobile_static
else:
app.template_folder = web_templates
app.static_folder = web_static
return
@app.route('/')
def index():
check_useragent
return render_template('index.html')
@app.route('/dashboard')
def dashboard():
check_useragent
return render_template('dashboard.html')
if __name__ == '__main__':
app.run()
I've tried every variation of relative path I can think of: <script src="../../../../dist/bundle.js"></script> <script src="/dist/bundle.js"></script>
with and without the first slash, and changing the directory various amounts of times. I can't seem to access this file.
If this is a simple fix (i.e. use an absolute path or move the file within static) let me know.. if not I'm assuming my static_folder setting in flask is keeping me from finding anything in the root directory or something?
source https://stackoverflow.com/questions/75101846/how-can-i-write-a-path-to-a-file-that-is-in-my-project-root-directory-inside-my
Comments
Post a Comment