Flask-多个文件夹中的应用

时间:2018-10-21 09:45:14

标签: python flask pythonanywhere

我正在尝试Flask和PythonAnywhere。我在学习时创建了许多Flask项目,我想从“主” Flask页面链接到它们。

Projects
-- Weather
---- app.py
---- static
---- templates

-- Coin Toss
---- app.py
---- static
---- templates

-- etc.

结合所有这些文件/文件夹的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

PythonAnywhere.com在初学者计划中仅支持一个Web应用。

最好的解决方案是创建新项目,并在app.py“ /” route(path)中向所有其他项目添加自定义路线。示例:

app.py“ / weather” ->路由到天气应用,其中Weather / app.py中的所有代码都放在/ weather路由

app.py“ / coin_toss” ->路由至Coin Toss应用,其中来自Coin Toss / app.py的所有代码均置于/ weather路由

app.py“ /” ->要选择“天气”或“抛硬币”或其他自定义路线(路径)上的项目。

编辑:示例您将如何做

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')
相关问题