Flask Hello World 入门

  1. 先安装 python3.x 环境, 如果你还没安装过的话.

在这里插入图片描述

把下面的代码保存为 init.sh
然后 chown +x init.sh

#!/usr/bin/env bash
#确保shell 切换到当前shell 脚本文件夹
current_file_path= ( c d " (cd " (dirname “$0”)"; pwd)
cd ${current_file_path}

rm -rf flask_tutorial
pwd;ls;
mkdir -p ${current_file_path}/flask_tutorial/flask/app/templates; cd ${current_file_path}/flask_tutorial;
virtualenv -p python3 --no-site-package venv
source venv/bin/activate
pip install flask

cd ${current_file_path}/flask_tutorial/flask/app;
touch init.py
touch routes.py
touch myblog.py

cat << EOF >> init.py
from flask import Flask
#创建app应用,__name__是python预定义变量,被设置为使用本模块.
app = Flask(name)
from app import routes
EOF

cat << EOF >> myblog.py
#从app模块中导入app应用
from app import app

#防止被引用后执行,只有在当前模块中才可以使用
if name==‘main’:
app.run()
EOF

cat << EOF >> templates/index.html

{{ title }} - 博客

Hello ,{{ user.username }},现在时间是:{{timenow.whattime}}!

EOF

#下面这个把上面的route.py 覆盖了.
cat << EOF >> routes.py
#导入模板模块
import time;
from flask import render_template
from app import app

@app.route(’/’)
@app.route(’/index’)
def index():
user = {‘username’:‘开心’}
timenow={‘whattime’: time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}
#将需要展示的数据传递给模板进行显示
#return render_template(‘index.html’,title=‘我的’,user=user)
return render_template(‘index.html’,title=‘我的’,user=user,timenow=timenow)
EOF

export FLASK_APP=myblog.py
flask run

猜你喜欢

转载自blog.csdn.net/happyfreeangel/article/details/84941412