vendredi 18 décembre 2020

Rendering two flask routes (of the same app) on the same page, independently of eachother

I am familiar with python, but I am very new to Flask.

Goal: My goal is to create a web front-end to some python code I have running on a server in an air-gaped network. This front-end would allow me to watching the stdout of a script that is running in the shell window while navigating through a file browser on the bottom portion of the page.

The initial app @app.route('/') is a textbox for directory path

Once that is submitted, the app moves to another page where there are actually 2 apps running on it. The top is called "/shell" and it runs hardcoded python scripts (for the purposes of my demo, I have just used ls -al path. The stdout of that command is piped to that same top portion of the page.

The bottom portion of the page is a directory browser (using the directory from the first page).

workflow:

enter image description here

I have the majority of the code written for the Flask side and I have muddled through most of the templates. I would like to have /shell render on the top section of the page and /browser render on the bottom portion independently of each other. I am not sure if I should do this as a Flask command or if this is done through a template somehow.

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'                                                              
                                                                                                 
 




Aucun commentaire:

Enregistrer un commentaire