I'm sure there are other ways to achieve this but one way is to use blueprint structure and contain all static files for that specific blueprint inside the blueprint folder.
app |- collections | - blueprint_one | - configs | - static | - css | - js | js_file_one.js | - templates | - blueprint_one | html_file_one.html | blue_print_one_view_file_one.py | __init__.py | - blueprint_two | - blueprint_three |- static | - js | - css |- templates __init__.py
Above folder structure allows you to separate out not only the static files but also the Flask view files.
Below are examples of activating/importing files:
1) app/init.py
app = Flask(__name__, static_folder='static') app.config.from_pyfile('app_config.py') # Do all your db set up and etcs.. # Import blueprints below set ups to prevent circular import app.collections.blueprint_one import blueprint_one app.collections.blueprint_two import blueprint_two # Now register blueprints that you've imported app.register_blueprint(blueprint_one, url_prefix='/blueprint_one') app.register_blueprint(blueprint_two, url_prefix='/blueprint_two')
2) blueprint_one/init.py
# Create a blueprint from flask import Blueprint blueprint_one = Blueprint('blueprint_one_name', __name__, template_folder='templates', static_folder='static') # Import required views to use from . import blue_print_one_view_file_one.py
3) blueprint_one/blue_print_one_view_file_one.py
# Import blueprint from app.collections.blueprint_one import blueprint_one # Any view functions using above blueprint @blueprint_one.route('/login/, methods=['GET']) def login(): return render_template('blueprint_one/html_file_one.html')
4) blueprint_one/templates/blueprint_one/html_file_one.html
// Note: instead of .static, you can use blueprint_one.static. <script type="text/javascript" src="{{ url_for('.static', filename = 'js/js_file_one.js') }}"></script>
5) app/static and app/templates still can be used as you are using it currently.
This solution doesn't solve the problem of putting js and html into same folder but at least you can divide each feature into component like blueprint for more modularized project structure.
templatesfolder tostaticfolder.