flask檔案路徑設定問題
阿新 • • 發佈:2020-12-21
服務默認了圖片和其他資源有指定資料夾,但是我們不習慣指定的資料夾,更喜歡在工程目錄下自定義資料夾地址存放不同的網頁資原始檔
關鍵
app = Flask( __name__, template_folder='.', # 表示在當前目錄 (myproject/A/) 尋找模板檔案 static_folder='', # 空 表示為當前目錄 (myproject/A/) 開通虛擬資源入口 static_url_path='', )
完整程式碼
#啟用環境 #!/usr/bin/env python2 # -*- coding: utf-8 -*- import time from flask import Flask, render_template, Response,request,redirect,url_for,jsonify,send_file, send_from_directory,json, jsonify,make_response from camera import VideoCamera import datetime,random #匯入時間和隨機數模組 import cv2 video=VideoCamera() app = Flask(__name__) import os pathnow=os.getcwd() pathnow=pathnow.replace('\\','/') #print(pathnow) #獲取當前工作目錄路徑 #print (os.path.abspath('mainPage0.html')) HTML_PATH=pathnow app = Flask( __name__, template_folder='.', # 表示在當前目錄 (myproject/A/) 尋找模板檔案 static_folder='', # 空 表示為當前目錄 (myproject/A/) 開通虛擬資源入口 static_url_path='', ) @app.route('/') def index(): return render_template('./215video/index_mp4.html') @app.route('/adjustPage') def add(): return render_template('adjustPage.html') @app.route('/login', methods = ["GET","POST"]) def login(): name = request.args.get("username") password = request.args.get("userpwd") print('待驗證賬戶:'+name+" 待驗證密碼:"+password) if name== 'admin' and password=='admin': #return redirect(url_for('use')) return '登入成功' else: return '登入失敗' @app.route('/setData', methods = ["GET","POST"]) def getvalue(): now = datetime.datetime.now().strftime('%H:%M:%S') data = {'time':now} return jsonify(data) #將資料以字典的形式傳回 #傳輸視訊 def gen(camera): while True: frame = camera.get_frame() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n') @app.route('/video_feed') def video_feed(): return Response(gen(video),mimetype='multipart/x-mixed-replace; boundary=frame') if __name__ == '__main__': app.run(host='0.0.0.0',port='8080')