I am trying to setup middlewares on my server for python so that subdirectories of blueprints can be seen by the apache2 on the server and served under a url like example.com/myapplicationsubfolder/routedblueprintfunction
my __init__.py inside /users/ folder looks like this:
class WebFactionMiddleware(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
environ['SCRIPT_NAME'] = '/myapplicationsubfolder'
return self.app(environ, start_response)
from index import application
application.wsgi_app = WebFactionMiddleware(application.wsgi_app)
After I made my app factory, I couldn’t use this anymore:
from app import app
app.wsgi_app = WebFactionMiddleware(app.wsgi_app)
And I am trying to get the flask app from index.py
Index.py is like this:
from app import create_app
application = create_app()
application.run()
app.py like this:
def configure_errors(app):
@app.errorhandler(500)
def internal_server_error(error):
return render_template('500.html'), 500
def create_app():
app = Flask(__name__)
app.config.from_object('config')
from users.views import b_users
db.init_app(app)
app.register_blueprint(b_users)
configure_errors(app)
return app
If anyone has a link to a large blueprinted-appfactory-implemented source code that uses WSGI middlewares for subdirectories for a python Flask website, it would be of great help.
I’m having some serious circular import problems or subdirectory init.py issues.
[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1] Traceback (most recent call last):
[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1] File "/home/somefolder/webapps/applicationsubfolder/htdocs/index.py", line 8, in <module>
[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1] from app import create_app
[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1] File "/home/somefolder/webapps/applicationsubfolder/htdocs/app.py", line 5, in <module>
[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1] from users.views import b_users
[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1] File "/home/somefolder/webapps/applicationsubfolder/htdocs/users/__init__.py", line 9, in <module>
[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1] from index import application
[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1] File "/home/somefolder/webapps/applicationsubfolder/htdocs/index.py", line 8, in <module>
[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1] from app import create_app
[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1] ImportError: cannot import name create_app
I am not entirely sure of your requirement. Let me try though.
If you want to prefix blueprint folder name before the view function url, do this:
app.register_blueprint(b_users, url_prefix=’/folder_name’)
Url will be
localhost/folder_name/urlMove this inside create_app method.