首页 > 解决方案 > 在同一页面上渲染两个烧瓶路线(同一个应用程序),彼此独立

问题描述

我熟悉 python,但我对 Flask 很陌生。

目标: 我的目标是为我在隔离网络中的服务器上运行的一些 python 代码创建一个 Web 前端。这个前端允许我在浏览页面底部的文件浏览器时查看在 shell 窗口中运行的脚本的标准输出。

初始应用程序@app.route('/')是目录路径的文本框

提交后,应用程序将移动到另一个页面,其中实际上运行了 2 个应用程序。顶部称为“/shell”,它运行硬编码的 python 脚本(为了我的演示,我刚刚使用了ls -al path。该命令的标准输出通过管道传送到页面的同一顶部。

页面的底部是目录浏览器(使用第一页中的目录)。

工作流程:

在此处输入图像描述

我有为 Flask 端编写的大部分代码,并且我已经搞砸了大部分模板。我希望 /shell 在页面的顶部呈现,而 /browser 在底部呈现,彼此独立。我不确定是否应该将其作为 Flask 命令执行,或者是否通过模板以某种方式完成。

import os
import subprocess
import json


app = Flask(__name__)
app.secret_key = 'development key'

@app.route('/', methods = ['GET', 'POST'])
def hello():
    form = target()
    if request.method == 'POST':
        if form.validate() == False:
            return render_template('index.html', form = form)
        else:
            pth = form.path.data
            return render_template('register.html', pth = pth)
    elif request.method == 'GET':
        return render_template('index.html', form = form)


FILE_SYSTEM_ROOT=pth

@app.route('/shell')
def sessions():
    return render_template('session.html')

@app.route('/send', methods=['GET', 'POST'])
def send():

    try:
        currpath = FILE_SYSTEM_ROOT
        if request.form['data']:
            currpath =  request.form['data']
        print(currpath)
        stdout, stderr  = subprocess.Popen(["ls","-al", currpath ], stderr=subprocess.PIPE, stdout=subprocess.PIPE).communicate()

        data = {}
        data['command'] = request.form['command']
        data['data'] = request.form['data']
        data['result'] = stdout.decode('utf-8') + "\n" + stderr.decode('utf-8')
        return (json.dumps(data))
    except Exception as e: print(e)

@app.route('/browser')                                                                           
def browse():                                                                                    
    itemList = os.listdir(FILE_SYSTEM_ROOT)                                                      
    return render_template('browse.html', itemList=itemList)                                     
                                                                                                 
@app.route('/browser/<path:urlFilePath>')                                                        
def browser(urlFilePath):                                                                        
    nestedFilePath = os.path.join(FILE_SYSTEM_ROOT, urlFilePath)                                 
    print(nestedFilePath)                                                                        
    if os.path.isdir(nestedFilePath):                                                            
        itemList = os.listdir(nestedFilePath)                                                    
        fileProperties = {"filepath": nestedFilePath}                                            
        if not urlFilePath.startswith("/"):                                                      
            urlFilePath = "/" + urlFilePath                                                      
        return render_template('browse.html', urlFilePath=urlFilePath, itemList=itemList)        
    if os.path.isfile(nestedFilePath):                                                           
        if not urlFilePath.startswith("/"):                                                      
            urlFilePath = "/" + urlFilePath                                                      
        with open(nestedFilePath,'r') as f:                                                      
            return render_template('file.html', text=f.read())                                   
    return 'something bad happened'                                                              
                                                                                                 
 

标签: pythonhtmlflaskwebjinja2

解决方案


推荐阅读