mohbar
mohbar

Reputation: 67

builtins.NameError NameError: name 'flask' is not defined

I am facing this problem while running my flask.py app:

Traceback:

File "/root/sitetoggle/lib/python3.4/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/root/sitetoggle/lib/python3.4/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/root/sitetoggle/lib/python3.4/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/root/sitetoggle/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/root/sitetoggle/lib/python3.4/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/root/sitetoggle/lib/python3.4/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/root/sitetoggle/lib/python3.4/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/root/sitetoggle/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/root/sitetoggle/lib/python3.4/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/root/sitetoggle/lib/python3.4/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/root/sitetoggle/flaskr/flaskr.py", line 117, in index
    return flask.Response(inner(), mimetype='text/html')  # text/html is required for most browsers to show th$
NameError: name 'flask' is not define

Here is my flask and template html

flask.py

import os
import subprocess
import request

from flask import (Flask, request, session, g, redirect, url_for, abort, render_template, flash)

app = Flask(__name__) # create the application instance :)
app.config.from_object(__name__) # load config from this file , flaskr.py

@app.route('/siteOFF', methods=['GET', 'POST'])
def siteoff():
   error = None
   subprocess.call(['/root/sitetoggle/flaskr/testOFF.sh'], shell=True)
   return render_template('layout.html', error=error)


@app.route('/siteON', methods=['GET', 'POST'])
def siteon():
    def inner():
        proc = subprocess.Popen(
            ['dmesg'],             #call something with a lot of output so we can see it
            shell=True,
            stdout=subprocess.PIPE
        )

        for line in iter(proc.stdout.readline,''):
            time.sleep(1)                           # Don't need this just shows the text streaming
            yield line.rstrip() + '<br/>\n'

    return flask.Response(inner(), mimetype='text/html')  # text/html is required for most browsers to show th$
if __name__ == "__main__":
    app.run()

index.html

<!doctype html>
<title>Flaskr</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
<div class=page>
<div>
<h4>Turn site OFF or ON </h4>
<input type="button" name="lien1" value="Turn ON site"
 onclick="self.location.href='http://10.10.1.8:5000/siteON'" style="background-color:#3cb371"
style="color:white; font-weight:bold"onclick>

<input type="button" name="lien2" value="Turn OFF site"
 onclick="self.location.href='http://10.10.1.8:5000/siteOFF'" style="background-color:#1ab171"
style="color:white; font-weight:bold"onclick>
</div>
</div>

I am using the flaskr app which I customized to call bash script within the flask page. Everything else is still working correctly except the two functions siteon() and siteoff() which I defined in my flask.py and trying to call them in the html page.

Upvotes: 0

Views: 3363

Answers (1)

JacobIRR
JacobIRR

Reputation: 8966

This is the bad line:

return flask.Response(inner(), mimetype='text/html')

So do:

from flask import (Flask, request, session, g, redirect, url_for, abort, render_template, flash, Response)

Then:

return Response(inner(), mimetype='text/html')

Upvotes: 1

Related Questions