'Flask - Apps in multiple folders
I am experimenting with Flask and PythonAnywhere. I have a number of Flask projects that I've created while learning, and I would like to link to them all from a 'master' Flask page.
Projects
-- Weather
---- app.py
---- static
---- templates
-- Coin Toss
---- app.py
---- static
---- templates
-- etc.
What is the best way to combine all these files/folders?
Solution 1:[1]
PythonAnywhere.com only supports one web app on the beginner plan.
Best solution would be to create new project and in app.py index route(endpoint) add custom routes to all other projects.
Example:
/weather -> Route to weather app, where all code from Weather/app.py is put in /weather route.
/coin_toss -> Route to Coin Toss app, where all code from Coin Toss/app.py is put in /weather route
/index -> To select Weather or Coin Toss or some other project on custom route(path).
EDIT:
Example how would you do it
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
# Just put all html in index.html
# return render_template('index.html')
return "<body>
<a href="{{ url_for('coin_toss') }}">Coin Toss</a>
<a href="{{ url_for('weather_app') }}">Weather App</a>
...
</body>"
@app.route("/coin_toss")
def coin_toss():
# Code for coin toss
return render_template('coin_toss.html')
@app.route("/weather_app")
def weather_app():
# Code for weather app
return render_template('weather_app.html')
Solution 2:[2]
Looking at the following Help page, it seems that you can in fact do as you want to do by using DispatcherMiddleware, allowing you to keep your applications in their original structure.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | rosebud |
