栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python学习 | 2021-10-22 Flask Web开发

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python学习 | 2021-10-22 Flask Web开发

目录

一、程序的基本结构

1、引入Flask类

2、创建Flask对象

3、编写主程序

4、路由

5、运行

二、Jinja2模板

三、实现视频分镜

运行结果:

​完整代码:

HTML语言:

四、问题记录与总结

1、pycharm运行flask报错

出现报错:

解决方案:

2、interpreter为invalid

出现报错:

解决方案:


一、程序的基本结构

1、引入Flask类

from flask import Flask

2、创建Flask对象
  • flask程序需要创建一个Flask类对象,用于应用的配置和运行
  • name 是Python中的特殊变量,如果文件作为主程序执行,那么__name__变量的值就是__main__,如果是被其他模块引入,那么__name__的值就是模块名称

app = Flask(__name__)

3、编写主程序
  • 在主程序中,执行run()来启动应用
  • 改名启动一个本地服务器,默认情况下其地址是localhost:5000,可以使用关键字参数port修改监听端口

if __name__ =="__main__":
    app.run(debug=True, port=5008)

4、路由
  • 使用app变量的route()装饰器来告诉Flask框架URL如何触发视图函数
  • 处理URL和函数之间关系的程序称为路由
  • 像index()这样的函数称为视图函数(view function),函数的返回值称为响应,是客户端会收到的内容
  • 对路径’/'的请求将转为对index()函数的调用

@app.route('/')
def index():
    return 'Hello World!'

  • 尖括号里的内容是动态部分

@app.route('/user/')
def user(name):
    return 'Hello, %s!' % name

5、运行

完整的flask程序:

from flask import Flask
app = Flask(__name__)

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

if __name__ == '__main__':
    app.run(port="5008")

二、Jinja2模板
  • 在项目中创建文件夹templates
  • 在templates里新建一个HTML文件,命名为index.html
  • 使用模板时,视图函数应当返回render_template()的调用结果
from flask import Flask,render_template
app=Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

if __name__ == '__main__':
    app.run(port="5008")
  • 模板文件index.html依赖于变量name



    
    Flask


Flask

三、实现视频分镜

运行结果:

完整代码:
from flask import Flask,render_template
import cv2
import os
import numpy as np
import matplotlib.pyplot as plt

app=Flask(__name__)

def vs():
    def classify_hist_with_split(image1, image2, size=(256, 256)):
        image1 = cv2.resize(image1, size)
        image2 = cv2.resize(image2, size)
        sub_image1 = cv2.split(image1)
        sub_image2 = cv2.split(image2)
        sub_data = 0
        for im1, im2 in zip(sub_image1, sub_image2):
            sub_data += calculate(im1, im2)
        sub_data = sub_data / 3
        return sub_data
    def calculate(image1, image2):
        hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0])
        hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0])
        plt.plot(hist1, color="r")
        plt.plot(hist2, color="g")
        degree = 0
        for i in range(len(hist1)):
            if hist1[i] != hist2[i]:
                degree = degree + (1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i]))
            else:
                degree = degree + 1
        degree = degree / len(hist1)
        return degree
    for i in range(549):
        img1 = cv2.imread('static/pic2/image{}.jpg'.format(i))
        img2 = cv2.imread('static/pic2/image{}.jpg'.format(i + 1))
        n = classify_hist_with_split(img1, img2)
        if (n < 0.6):
            cv2.imwrite('static/shot/image{}.jpg'.format(i + 1), img2)
    #重命名挑选出的分镜图片
    path = "D:python learningpycharmstaticshot"
    filelist = os.listdir(path)
    count = 0
    for file in filelist:
        Olddir = os.path.join(path, file)
        if os.path.isdir(Olddir):
            continue
        filetype = os.path.splitext(file)[1]    #分出'jpg'
        Newdir = os.path.join(path, str(count).zfill(1) + filetype)    #zfill()参数决定名称位数
        os.rename(Olddir, Newdir)
        count += 1

@app.route('/')
def index():
    vs()
    pic='static/shot/'
    framecount=6
    return render_template('index.html',pic1=pic,framecount=framecount)

if __name__ == '__main__':
    app.run(port="5008")



    
    Flask分镜


视频分镜


分镜头:
{% for i in range(framecount) %} {pic1}}{{i}}.jpg" /> {% endfor %}

HTML语言:
        {{ }}        变量        {% %}        代码

四、问题记录与总结

1、pycharm运行flask报错

出现报错:

ModuleNotFoundError: No module named 'flask'

解决方案:
  1. 打开左上角File->Settings->PROJECT:xxx->Project Interpreter
  2. 打开右边的Project Ineterpreter:的下拉列表,选择Python 3.7
  3. 如果打开python3.7一片空白,就点击右下角的Apply,然后点击OK

2、interpreter为invalid 出现报错:

解决方案:
  1. 打开左上角File->Settings->PROJECT:xxx->Project Interpreter
  2. 打开右边的Project Ineterpreter:的设置,选择show all
  3. 单击右侧的“-”,移除invalid的interpreter
  4. 点击 “+”,添加正确的路径

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/341311.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号