Creating a new Flask project with pipenv

  • Create the pipenv environment
$ mkdir project
$ cd project
$ pipenv install
$ mkdir project
$ cd project
$ pipenv install
$ mkdir project $ cd project $ pipenv install

Enter fullscreen mode Exit fullscreen mode


  • Install Flask
$ pipenv install flask
$ pipenv install flask
$ pipenv install flask

Enter fullscreen mode Exit fullscreen mode


  • Structure folders/files:
mkdir app
touch app/__init__.py
touch app/routes.py
touch microblog.py
mkdir app
touch app/__init__.py
touch app/routes.py
touch microblog.py
mkdir app touch app/__init__.py touch app/routes.py touch microblog.py

Enter fullscreen mode Exit fullscreen mode


  • In app/__init__.py:
from flask import Flask
app = Flask(__name__)
from app import routes
from flask import Flask

app = Flask(__name__)

from app import routes
from flask import Flask app = Flask(__name__) from app import routes

Enter fullscreen mode Exit fullscreen mode


  • In app/routes.py:
from app import app
@app.route('/')
@app.route('/index')
def index():
return "Hello, World!"
from app import app

@app.route('/')
@app.route('/index')
def index():
    return "Hello, World!"
from app import app @app.route('/') @app.route('/index') def index(): return "Hello, World!"

Enter fullscreen mode Exit fullscreen mode


  • In microblog.py:
from app import app
from app import app
from app import app

Enter fullscreen mode Exit fullscreen mode


  • Change into the pipenv:
$ pipenv shell
$ pipenv shell
$ pipenv shell

Enter fullscreen mode Exit fullscreen mode


  • Set the FLASK_APP variable:
$ export FLASK_APP=microblog.py
$ export FLASK_APP=microblog.py
$ export FLASK_APP=microblog.py

Enter fullscreen mode Exit fullscreen mode


  • Run the app:
$ flask run
$ flask run
$ flask run

Enter fullscreen mode Exit fullscreen mode


Now, whenever you need to extend your Flask app to import some new libraries or framework or whatever, just install it using pipenv.


This whole process inspired me to write a bash script that does all this automatically for you. Why make things harder than they need to be?

Grab autoflask.sh and give it a spin on your system to quickly get a barebones Hello World project in Flask started today!


If you need a Computer Science tutor, code reviewer, or just someone to pair program with, hit me up

原文链接:Creating a new Flask project with pipenv

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享
Change your thoughts and you change your world.
改变你的思想,你就能改变自己的命运
评论 抢沙发

请登录后发表评论

    暂无评论内容