PythonAnywhere Forums

Use of cherrypy, wsgi configuration and access to standard folders to transfer localdev to production

Hi everyone, I am currently testing a simple deployment of a simple website on python anywhere written with cherrypy. The thing is, as mentionned, cherrypy has an internal server, therefore we need to configure the wsgi file. Once I configured the wsgi file in order to launch a simple web page, it worked, but, let's assume I want to move all my files in the standard directory, and at the same time, redirect the wsgi configuration towards that directory. Do you know any example that could allow me to do it ? With this question, my aim is simply to move my developments I have on the local, and place them on the server and play with them.
It seems some solutions using Flask or any other framework, could also be considered, however, any idea on how I could do with cherrypy ? Have a nice day, G

Check out our help pages. In particular this article.

@conrad:Thanks for your advice. Actually I did it and implemented it. The thing is, well, I don't think it would be a good practice to put all my developments on the wsgi file. Therefore, there must be something else. Would you agree ? I mean, somehow, I should be able to redirect the developments to the folder. Any further advice ?

You could put the code that's in your WSGI file in another file, and then import it. So, for example, if you had the sample code from the page that Conrad linked to:

import sys
sys.stdout = sys.stderr

import atexit
import cherrypy

cherrypy.config.update({'environment': 'embedded'})

if cherrypy.__version__.startswith('3.0') and cherrypy.engine.state == 0:
    cherrypy.engine.start(blocking=False)
    atexit.register(cherrypy.engine.stop)

class Root(object):
    def index(self):
        return 'Hello World!'
    index.exposed = True

application = cherrypy.Application(Root(), script_name='', config=None)

You could put the application code in a file -- say, /home/gbst/mysite/myapp.py, containing this stuff (note that I've moved the setup stuff into a function):

import atexit
import cherrypy

def setup():    
    cherrypy.config.update({'environment': 'embedded'})

    if cherrypy.__version__.startswith('3.0') and cherrypy.engine.state == 0:
        cherrypy.engine.start(blocking=False)
        atexit.register(cherrypy.engine.stop)


class Root(object):
    def index(self):
        return 'Hello World!'
    index.exposed = True

...and then in the WSGI file, add the directory containing that code to the system path, import it, and create the application:

import sys
sys.stdout = sys.stderr

import cherrypy

code_directory = "/home/gbst/mysite/"
if code_directory not in sys.path:
    sys.path.insert(0, code_directory)

from myapp import Root

application = cherrypy.Application(Root(), script_name='', config=None)

@giles: thanks for your answer, I am going to test it. At first glance, I think it will help me.