Leyo R
Leyo R

Reputation: 723

How to display command output in Flask using the Subprocess python module

Below is my code

view.py

from flask import render_template
#Import variables stored in _init_.py
from app import app

@app.route('/')
@app.route('/index')
def index():
import subprocess
memory = subprocess.Popen(['date'])
out,error = memory.communicate()

return render_template('view.html', out=out, error=error)

view.html

{{ out }}

It prints none, none, I am not sure what's wrong as it works fine in the python script

Update : Thanks for the answers, this is how you can do it

Upvotes: 1

Views: 4602

Answers (1)

Nonnib
Nonnib

Reputation: 467

If you want to grab stdout from your command you need to add stdout=subprocess.PIPE as in:

memory = subprocess.Popen(['date'], stdout=subprocess.PIPE)

Upvotes: 2

Related Questions