Server Error in Default Deployment of Azure Flask Web App - python

I'm trying to deploy a Flask app within Azure, using the pre made template created by Azure.
I can't get a simple...
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run()
To do anything. To simplify further still, even the pre made deployment returns an error...
Clearly I'm misunderstanding something very fundamental here.
Using FTP I can see the files that Azure is creating - sure does look like there should be a nice little demo site.
What am I doing wrong?
Cheers, Ben

According to your description, I think the issue was caused by some incorrect configuration in the web.config & the Application Settings of your WebApp settings. Please try to refer to the blog to resolve it.
Meanwhile, I don't know what pre made template created by Azure you used. I tried to create a new Azure WebApp for Flask as below.
The file list in the wwwroot path in the Kudu tool or FTP shown as below.
Then, I modified the code views.py in the FlaskWebProject1 directory. And it works.
It seems to be more simple. Hope it helps.

I've been figthing the same issue for a while, I have only be able to workaround it by changing the pricing tier associated to the "service plan" to S2 Standard. If I try to use S1 Standard I run into the same issues.

Related

Python Flask "The configuration file does not exist." accessing sub-path

I am deploying Flask on CPanel, and basic configurations are working correctly,
up to the point that I can access home route, defined as:
#app.route("/home")
#app.route("/")
def home():
return "This is home page, blah blah blah"
The site can be thought of, as being accessed at
https://sub-domain.main-domain.com/base-url,
thus, home page is accessed as https://sub-domain.main-domain.com/base-url/ and that works perfectly.
Problem
So, the issue comes when I access any other route, other than the slash("/"),
in the above example, for instance, accessing
https://sub-domain.main-domain.com/base-url/home,
doesn't seem to work at all, and results in
The configuration file does not exist. error.
Any possible help would be kindly appreciated. my passenger_wsgi.py is configured as:
import sys
# add your project directory to the sys.path
base_dir = u'/home/cpaneusername/base-dir'
if base_dir not in sys.path:
sys.path = [base_dir] + sys.path
# import flask app but need to call it "application" for WSGI to work
from app import website as application
Nevertheless, thanks for the assistance.
What I can suggest resolving the issue is to change the base URL of your Python app and remove any sub-folders from it. For example, just keep it that way:
https://sub-domain.main-domain.com/
And try again.
Otherwise, you might want to change the /home route of your app to this one instead:
#app.route("/base-url/home")
If none of the above works, then make sure that you have the correct .htaccess rules in place to forward the requests properly to your app, but I assume you already have considering the fact that you are able to access the / of the app.
I tried moving the Python app and other supporting modules to /public folder, and in the wsgi file, I imported as
from public.app import website as application
There, it worked.

Is it possible to change code of flask without rerunning the flask server after deployment?

Consider, I have a flask server deployed which routes to multiple webpages. If I want to change content of one route, by changing its code, is it possible to reflect those changes in webpages without rerunning the flask server? It is possible to host and rerun other scripts on the linux server or entriely another flask server as long as the website url(port number and route) doesn't change.
Please suggest any way you can come up with!
setting flask environment will do the thing for you in the shell.
export FLASK_ENV=development
Assuming you are using flask development server,
Yes, its possible using use_reloader=True,
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
# You business logic code
return some_data
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port=8000,
use_reloader=True, threaded=True)
However it is not good idea to use it in production, related Is the server bundled with Flask safe to use in production?
My assumption is that you want your deployment service to recognize that a change has occurred in a file, which prompts it to automatically restart to reflect the changes, rather than you manually restarting the service.
Assuming that you are deploying on a production uWSGI server, starting the service with touch-reload=/path/to/folder would allow uWSGI to reload when the specified file/folder is modified or touched. https://uwsgi-docs.readthedocs.io/en/latest/Options.html#touch-reload
--touch-reload=/path/to/file.txt
I think you can only do this with the Apache Webserver. Refer to the Flask documentation.
I haven't tried it (yet), but when you have deployed your new code any small change in the wsgi-file should automatically reload the code.
You mentioned two requirements
I have a flask server deployed
I would assume you mean it is deployed in a production environment. This automatically rules out debug-mode from the development server, because it is not stable, efficient or secure for production use.
Without rerunning the flask server
This rules out autoreload, because this will completely restart your server. Any requests that come in until the server is ready again will fail.
One way of going about is by running your application with debug mode ON through app.run(debug=True)
The problem with this approach is that your application would be exposed over the internet with the application's internal debugger on which shouldn't be done.
A better way I can think of is to call the functions you need to change frequently from a different file other than where your core flask code exists and change in that file whenever needed. This way you don't need to change your Flask code and you won't need to restart the application
Edit:
A sample Flask route would be
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
# Route's business logic usually goes here
return data
Now, instead of writing your application's business logic in the same file as your route, you could simply write it in a different file as follows and import it in the file with the route:
File with the business logic
def process_hello_world(params):
# Write your business logic here which you want to change without reloading the application
return data
And in the application file with the route call the business logic as follows:
from flask import Flask
from
app = Flask(__name__)
#app.route('/')
def hello_world():
data = process_hello_world()
return data

How to serve static files WebApp2 NO Google AppEngine

I am trying to use WebApp2 outside of AppEngine and can't find anywhere in the documentation how to set up static routes to files.
For example, I have the following folder structure
Presentation
-->js
-->-->main.js
-->templates
-->-->index.html (loaded via Jinja)
How do I reference the main.js as using ../js/main.js gives me a 404
Here's the documentation that deals with this: Quick start (to use webapp2 outside of App Engine). Did this not work?
Serving static files should occur outside of your WSGI application (the webapp2 app), so it depends on what server you are using to run your webapp2 app (which is a WSGI app), because that server will need to be configured to serve the static files. Could you please add to your question what kind of server you are using, and/or how you are running the webapp2 app?
Appreciate your response, found similar after much digging. Problem was my lack of understanding of exactly what WebApp2 was as normal point AppEngine Dev App Server at it.
I only need a simple HTTP server, so doing the following
static_app = StaticURLParser("Presentation/")
# Create a cascade that looks for static files first, then tries the webapp
app = Cascade([static_app, web_app])
def main():
httpserver.serve(app, host='127.0.0.1', port='8080')
if __name__ == '__main__':
main()
and then run python main.py

Catch-all URL with Flask when the path also contains a URL

Ok, so I want to catch all the URLs in my Flask app, with this piece of code
#app.route('/<path:path>')
def catch_all(path):
return path
Just a basic app with shows the relative path of each page. So this works fine for most of the paths (localhost/whatever/I/want for example prints whatever/I/want).
EXCEPT in one case. For example if I type localhost/foo/http://google.com/bar in the URL bar, what I expected to be shown is foo/http://google.com/bar, but what I actually get is only bar. So actually Flask finds the last valid URL and takes the path following this URL.
Is there anyway to catch the real path of our URL?
Edit: I'm running Flask 0.10 using the Google App Engine for Python.
I was able to reproduce your problem with the current Google App Engine SDK; this is caused by the way the bundled CherryPy server parses the request.
The problem applies only to your local dev server, when you deploy the app to Google, a different handler parses the URI and the http:// scheme is left unparsed.
See CherryPy issue #1284 as well as the corresponding appengine devserver ticket.
The work-around is to URI-encode the colon:
http://localhost:8000/foo/http%3A//google.com/bar
works.

Trying to get Mongrel2 + m2wsgi to work

I'm trying to get mongrel2 to work with m2wsgi. What I need to do so I can see "Hello World!" in my browser? Mongrel2 is installed but any site configurations are not done yet.
def app(environ, start_response):
start_response("200 OK", [('Content-Type', 'text/plain')])
return ['Hello World!', ]
Trying to run it:
m2wsgi test
AssertionError: the specified app is not callable
I'm running Ubuntu Maverick.
If Mongrel2 is not fully configured, as your "Mongrel2 is installed but any site configurations are not done yet" statement suggests, then it won't be able to find your app (the m2wsgi documentation is perhaps not as clear about this as it might be). Here's a tutorial on getting Mongrel2 set up and ready to connect to a WSGI app - it uses wsgid instead of m2wsgi, but I bet you can adapt it to your needs.
To m2wgi be able to load your app, it must be in your PYTHONPATH, so to be able to run your test app try copying the test.py module to somewhere in your PYTHONPATH or, easier, try this:
PYTHONPATH=.:$PYHTONPATH m2wsgi test.app tcp://127.0.0.1:9995
Assuming you have, in your mongrel2 config database, a route pointing to a handler with send_spec = tcp://127.0.0.1:9995 and recv_spec = tcp://127.0.0.1:9994. I tried this locally and it worked:
daltonmatos#jetta ~ [6]$ curl http://localhost/m2wsgi/
Hello World!daltonmatos#jetta ~ [7]$
Take a look at the blog post cited by Sean, you have a great idea on how to configure mongrel2 and setup your hosts/routes/handlers. Also give wsgid a try, maybe you like it =). It also has support for raw WSGI apps.
Gook luck and happy hacking!

Categories

Resources