Forums

I seem to have a problem hosting my flask app

I have a file index.html which is in the templates folder which i created here. I have another style.css file in the static folder which also i created here. I have a routes.py file which is not in any folder so i left it the same way here. Now here are my questions:

1.What should the app.run() method look like in my main() in my routes.py file? Should i leave it as app.run() or should i make it as app.run(host='0.0.0.0') is it is on my computer?

  1. My python file creates an HTML document on my C:// drive in the templates folder and stores it on it. Here on pythonanywhere should i change it? On my system it creates a status.html file in the templates folder, the code looks like that : def browseLocal(webpageText, filename='C:\Python27\Programs\FlaskApp\templates\status.html'):

Should i change that to def browseLocal(webpageText, filename='/home/akshaybolakani25/mysite/templates/status.html'):

  1. My error log says that "No Module named HTML" I have used HTML.py to create an html file, should i be uploading any files from that library on pythonanywhere?

  2. Even though i changed the "from flask_app import app as application" to "from routes import app as application" in my akshaybolakani25_pythonanywhere_com_wsgi.py file it still gives me the error that "No module named flask_app" in the error log.

My app is relatively simple as it doesn't have any database. Can someone help me with how to host my flask app?

Thanks.

What should the app.run() method look like in my main() in my routes.py file? Should i leave it as app.run() or should i make it as app.run(host='0.0.0.0') is it is on my computer?

You shouldn't call app.run() at all on PythonAnywhere -- it's only for when you're running Flask in debug mode on your own machine. So long as you set up the WSGI file correctly, PythonAnywhere's backend systems will handle all of the stuff that app.run() does for you, in a more scalable and secure manner. Normally people "hide" the run from PythonAnywhere using code like this:

if __name__ == "__main__": 
    app.run()

The condition will evaluate to False on PythonAnywhere, so it will work OK.

My python file creates an HTML document on my C:// drive in the templates folder and stores it on it. Here on pythonanywhere should i change it? On my system it creates a status.html file in the templates folder, the code looks like that : def browseLocal(webpageText, filename='C:\Python27\Programs\FlaskApp\templates\status.html'):

Should i change that to def browseLocal(webpageText, filename='/home/akshaybolakani25/mysite/templates/status.html'):

Yes, that's right. I should say, though, that writing stuff to your templates directory is a little unusual in a web app.

My error log says that "No Module named HTML" I have used HTML.py to create an html file, should i be uploading any files from that library on pythonanywhere?

You can install it for your account by running this from a bash console:

pip2.7 install --user html

(Change the "2.7" to "3.3" or "3.4" if you're using a different Python version.)

Even though i changed the "from flask_app import app as application" to "from routes import app as application" in my akshaybolakani25_pythonanywhere_com_wsgi.py file it still gives me the error that "No module named flask_app" in the error log.

Can I take a look at your files? We can see them from our side, but we always ask permission first.

Giles,

You can take a look at my files. I installed the html and also made all the other changes you suggested but still it doesn't work as of now. Your help would be much appreciated. My email is akshay.bolakani@gmail.com if you wanted to access my account.

Thanks, Akshay

OK, if you look at the error log (linked from the "Web" tab) you'll see that the most recent error (at the bottom) is this:

2016-03-07 17:44:33,959 :ImportError: No module named HTML

Looking at the documentation for the html.py library it looks like you're meant to import it like this:

from html import HTML

There are two possibilities here:

  1. Maybe we're talking about different HTML libraries. Does the one I linked to above look like the right one?
  2. If we're talking about the same one, perhaps your code to import it is incorrect -- it's possible that on Windows, the import is happening without case sensitivity (that's not abnormal on Windows). But on a Linux-based environment like PythonAnywhere, filenames are case-sensitive, so you need to import it using lower-case letters.

Giles,

I changed that and it seems to be loading on the server but still the this part is not working :

"My python file creates an HTML document on my C:// drive in the templates folder and stores it on it. Here on pythonanywhere should i change it? On my system it creates a status.html file in the templates folder, the code looks like that : def browseLocal(webpageText, filename='C:\Python27\Programs\FlaskApp\templates\status.html'): Should i change that to def browseLocal(webpageText, filename='/home/akshaybolakani25/mysite/templates/status.html'):"

I don't see it creating a "status.html" file in my templates folder so it's not rendering that file, when i click the button to get that file nothing happens. When i checked the log, it says Internal Server Error 500.

What do you suggest?

Check out the bottom of the error log -- it says this:

2016-03-07 18:03:04,405 :  File "/home/akshaybolakani25/mysite/routes.py", line 66
2016-03-07 18:03:04,405 :    
2016-03-07 18:03:04,405 :               ^
2016-03-07 18:03:04,405 :IndentationError: expected an indented block

[edit] where are you seeing the "Internal Server Error 500" message?

I don't think it's an indentation error, it's getting deployed on the server, just that it's not executing the command where it has to go and create a new html file and put it in the templates folder for it to be returned to the frontend. Also i saw the 500 error on the console on chrome dev tools on the browser.

There definitely is an indentation error somewhere -- the Python interpreter doesn't tend to lie about such things. It might just not be an obvious one. Have you reloaded the website (on the "Web" tab) since you updated the code?

I checked all the indentation, this is the new error:

Traceback (most recent call last): File "/home/akshaybolakani25/mysite/routes.py", line 51, in <module> app.run() File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 772, in run run_simple(host, port, self, **options) File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 710, in run_simple inner() File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 692, in inner passthrough_errors, ssl_context).serve_forever() File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 486, in make_server passthrough_errors, ssl_context) File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 410, in init HTTPServer.init(self, (host, int(port)), handler) File "/usr/lib/python3.4/socketserver.py", line 430, in init self.server_bind() File "/usr/lib/python3.4/http/server.py", line 133, in server_bind socketserver.TCPServer.server_bind(self) File "/usr/lib/python3.4/socketserver.py", line 444, in server_bind self.socket.bind(self.server_address) OSError: [Errno 98] Address already in use

What does this mean now?

Are you trying to run your Flask app from a bash console? I can't see that error anywhere in your error log.

Inside the file where i can edit it, there is an option to "Run this File" although it is using Python 3.4 i think, my code is in Python 2.7

You shouldn't use the "run this file" option for a web app. That will just try to run the code in a console, which won't change the hosted website.

Instead, once you've saved your changes, click the "Reload" button that's near the top right to reload the website, then visit it in a browser. If you get an error, look at the logs that are linked from the "Web" tab. (They can take a few seconds to update.)

Did that, this is what the error log looks like:

2016-03-07 18:49:07,546 :Starting new HTTP connection (1): proxy.server
2016-03-07 18:49:07,549 :Exception on /getStatus [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1360, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1358, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1344, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/akshaybolakani25/mysite/routes.py", line 47, in getStatus
    loadXml(service)
  File "/home/akshaybolakani25/mysite/routes.py", line 14, in loadXml
    dom = parseString(response.content)
  File "/usr/lib/python2.7/xml/dom/minidom.py", line 1928, in parseString
    return expatbuilder.parseString(string)
  File "/usr/lib/python2.7/xml/dom/expatbuilder.py", line 940, in parseString
    return builder.parseString(string)
  File "/usr/lib/python2.7/xml/dom/expatbuilder.py", line 223, in parseString
    parser.Parse(string, True)
ExpatError: no element found: line 29, column 7

[edited by admin: formatting]

Are you accessing an outside site from your webapp? If so you need a paying account.

See this for a similar situation.