Forums

Is it possible to reset all files and settings?

I tried to uninstall a django app automatically installed by this site by manually deleting the directory which was created (pretty sure that's not the best idea but it's what I did. Sorry). Then tried to set up a flask app like:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

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

in my wsgi file (I also deleted the django stuff) and hit the reload web app button but I get an internal server error The error log says:

[Sun May 06 12:28:30 2012] Traceback (most recent call last):
[Sun May 06 12:28:30 2012]   File "/bin/serve_wsgi.py", line 53, in main
[Sun May 06 12:28:30 2012]     application = get_application_with_error_logger(error_log)
[Sun May 06 12:28:30 2012]   File "/bin/serve_wsgi.py", line 47, in get_application_with_error_logger
[Sun May 06 12:28:30 2012]     return ErrorLoggingWrapper(wsgi_module.application, error_log)
[Sun May 06 12:28:30 2012] AttributeError: 'module' object has no attribute 'application'

Is it possible to just start again with files and settings as when I signed up? Though I may learn something if I can fix this.

What should I do?

I seems to be something with how im trying to use flask

I can get a hello world page fine using this in the wsgi file:

import web
urls = ('/', 'index')

class index:
    def GET(self):
        web.header('Content-Type','text/html; charset=utf-8', unique=True)
        return """
            <html>
                <head>
                    <title>Python Anywhere hosted web application</title>
                </head>
                <body>
                    <h1>Hello, World!</h1>
                </body>
            </html>
        """

# comment out these two lines if you want to use another framework
app = web.application(urls, globals())
application = app.wsgifunc()

Yup, you've worked it out. Basically, the wsgi.py file needs to define a variable called application, which is then used to serve up your web app. This is different to the Flask web.application class, but your code is the right way of converting from one to the other.

I guess the natural next step for your app would be to have the Flask code sitting somewhere in your home directory so that you could more easily extend it and add other classes. The way you'd do that is have a file like this in, say, /home/chrisj/hello_world.py:

import web
urls = ('/', 'index')

class index:
    def GET(self):
        web.header('Content-Type','text/html; charset=utf-8', unique=True)
        return """
            <html>
                <head>
                    <title>Python Anywhere hosted web application</title>
                </head>
                <body>
                    <h1>Hello, World!</h1>
                </body>
            </html>
        """

app = web.application(urls, globals())

....and then, in your wsgi.py you'd be able to import it with code like this:

import sys

code_dir = '/home/chrisj'
if code_dir not in sys,path:
    sys.path.append(code_dir)

import hello_world
application = hello_world.app.wsgifunc()

Hope that helps!

Thanks for your help

I still didn't quite get how to use the flask framework on this server but have since ben playing around with pyramid. Pyramid is great!

There are some more clues in the error logs and I think behind the scenes Flask was trying to use its own HTTP server even though I didn't think I was asking it to. I was just trying this:

from flask import Flask
application = Flask(__name__)

@application.route("/")
def hello():
    return "Hello World!"

application.run()

Flask docs don't easily show me how to use a wsgi handler for the variable application

nevermind, just realised all I needed to do was remove the run method

Thanks for posting -- that sounds exactly right.