Reputation: 2860
I am packaging up my Flask app and I am unable to resolve static URLs after migrating the app.py into the module ship_comm even though it is picking up the new templates path when I change the root_folder. Here is my structure:
├── README.md
├── requirements.txt
├── setup.py
└── ship_comm
├── app.py
├── config.py
├── database.py
├── db_models.py
├── devices.py
├── replay.py
├── router.py
├── routing.db
├── static
│ ├── css
│ │ └── main.css
│ └── img
│ ├── captain-ai-logo-small.png
│ ├── captain-ai-logo-square.png
│ ├── favicon.ico
│ └── screenshot.png
└── templates
├── devices.html
├── home.html
├── layout.html
├── messages.html
├── new_route.html
├── new_serial.html
├── new_udp.html
└── routes.html
And here is my initialization of the Flask app
app = Flask(__name__, root_path='ship_comm')
This fixed my issue with TemplateNotFound error, but still it's not able to find the static folder, even though it should be able to find it in the root_path right? I have tried explicitly stating static_folder='ship_comm/static'
and static_folder='static'
but that also didn't help.
I call the app by using python -m ship_comm.app
Here is my layout.html that is now missing the logo:
<!doctype html lang="en">
<link rel="shortcut icon" href="{{ url_for('static', filename='img/favicon.ico') }}">
<title>Ship Comm Router</title>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<!-- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> -->
<!--- mobile stuff --->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
</head>
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
<div class="container-fluid">
<a href="/"><h1>Ship Comm Router</h1></a>
{%- for category, message in get_flashed_messages(with_categories=true) %}
<p class="flask {{ category }}-flash">{{
"Error: " if category == 'error' }}{{ message }}</p>
{%- endfor %}
<hr>
{% block body %}{% endblock %}
<hr>
<div id="footer">
<!--<img width="50px" src="../static/img/captain-ai-logo-small.png"/>-->
<img width="50px" src="{{url_for('static', filename='img/captain-ai-logo-small.png')}}"/>
Developed by Captain AI - 2018
</div>
</div>
<!-- <address>Flask and SQLAlchemy powered Serial and UDP Router</address> -->
I am using Python 3.6.7 and Flask==1.0.2
Upvotes: 2
Views: 1262
Reputation: 11
I had a similar structure to yours and ran into this issue when trying to follow a tutorial to create a Docker container for my small webapp. I eventually want to deploy this container to a QNAP to see if I can host things myself. What worked for me was:
from flask import Flask, render_template
app = Flask(__name__, template_folder='../templates', static_folder="../static")
Upvotes: 1
Reputation: 1937
root_path
should be automaticaly calculated. If you want to set it, try to use absolute path (assuming you create the app in app.py
:
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
app = Flask(__name__, root_path=dir_path)
Then your static files should be working.
Upvotes: 3