python flask code - on windows through HTTP request - python

I have come with python flask code which uses python libraries pywin32 and pYAD. I need to run these in production enviornment.
I tried to enable it over IIS through wfastcgi but its not working as expected.
Is there a way I can leverage the script to use in production set up, I have tried using Always Up converting script as a service but its not allowed in my organization similar with NSSM.
Is there any other way to explore this. Kindly help
Thanks

You can try waitress. You'll find the documentation Here.
Install Waitress
pip install waitress
setup.py file
from waitress import serve
import main
serve(main.app, host='0.0.0.0', port=8080) #'main.app' being your entry point
run setup.py and access your app through http://host_ip_address:8080

Related

Pandas failing to install when deploying Dash app to Azure

I'm getting the following error message when trying to deploy my Dash app to Azure:
Error
It then cleans up and says that "Command pyton setup.py egg_info failed with error code 1 in D:\home\site\wwwroot\env\build\Pandas"
What am I doing wrong here? Is there an issue with Pandas?
Funnily enough I ran into the exact same issue for a web app that I am working on at the moment. After 5 days of trying endless solutions I have eventually managed to get my app to deploy to Azure. My app is a Flask web app but the process is pretty much the same (if you are using Django or Dash in your case) or anything else. I am providing my answer based on the most useful links that solved my issue (I checked far too many, but these did the trick!).
This seems to be a known issue with Azure and is to do with Python version and package compatability. The first thing to check here will be the version of python you are using - if you have created your virtual environment in a version of python which is > 3.4 then you will need to install the Azure Python extension. This can be found on the left hand pane of your App Service resource under the category 'Development Tools' -> 'Extensions'. Currently the latest Python extension you can install is version 3.6.4. I had to install this as I was using python 3.6.5 for my web app:
I used the following answer by Konrad Lyda to help me solve my issue : Using python 3.6 on azure app services - not working despite it is installed as extension. You will have to manually install your packages using the kudu console and by adding a .skipPythonDeployment file. This is all explained in the link. I managed to replicate the same WSGI_HANDLER Error as highlighted and some further research led me to this link: https://github.com/Cojacfar/FlaskWeb . I know this link is Flask specific but the project structure should really help. You will need to add a web.config file to your project (just take the code from the web.config file in the git project) and replace the 'WSGI_HANDLER' value to the name of your app file. My app was defined in my views.py file so the value I used was 'views.app'.
The line that did it for me was:
wsgi_app = app.wsgi_app
which goes under your app declaration. For example, as I was using Flask and Flask boostrap I have the following in my app file:
app = Flask(__name__)
bootstrap = Bootstrap(app)
wsgi_app = app.wsgi_app
Once I got all my code in place I removed any unnecessary files that I did not need and deployed to Azure and it started working!
Let me know you get on. Hope this helps.

Importing mysql.connector into Azure Flask Project

I'm trying to deploy a Flask web app with mysql connectivity. It's my first time using Azure, and coming off Linux it all seems pretty confusing.
My understanding is that one includes within the requirements.txt to include the packages required. When I build the default Flask app from Azure the file looks like this:
Flask<1
At this stage the site loads fine.
If I then include an additional line
https://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.1.14.tar.gz
As per this answer https://stackoverflow.com/a/34489738/2697874
Then in my views.py file (which seems to be broadly synonymous to my old app.py file) I include...import mysql.connector
I then restart and reload my site...which then returns the error The page cannot be displayed because an internal server error has occurred.
Error logging spits out a load of html (seems pretty weird way to deliver error logs - so I must be missing something here). When I save to html and load it up I get this...
How can I include the mysql.connector library within my Flask web app?
Per my experience, the resoure https://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.1.14.tar.gz is for Linux, not for Azure WebApps based on Windows, and the link seems to be not available now.
I used the command pip search mysql-connector to list the related package. Then, I tried to use mysql-connector instead of mysql-connector-python via pip install, and tried to import mysql.connector in local Python interpreter that works fine.
So please use mysql-connector==2.1.4 instead of mysql-connector-python== in the requirements.txt file of your project using IDE, then re-deploy the project on Azure and try again. The package will be installed automatically as the offical doc said as below.
Package Management
Packages listed in requirements.txt will be installed automatically in the virtual environment using pip. This happens on every deployment, but pip will skip installation if a package is already installed.
Any update, please feel free to let me know.

Flask app doesn't builds on MS Azure cloud

I want to deploy my flask web application on Azure cloud. In Deployment options, I have selected GitHub as source destination for my flask code. after doing the configuration test successfully, the init.py file now starts building;
Now when I go to my application link, it shows me this;
Now at this point, I went back to my deployment options, it says Building failed;
the log generated for this building failed can be seen in the first picture. All the tests has passed except the last one "Performance test". Have anyone encountered the same issue before ? what can be the reason for that ?
I am running the application on localhost # port 8000.
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
Do I need to run it on another IP ?
You cannot listen on port 8000 in Web Apps. Only port 80 or 443. You'll need to read the port number from the environment, to know what to listen on.
If you created the Azure Webapp using the Flask tool, the default app is called FlaskWebProject1. If your app has a different name, you need to modify web.config in your wwwroot folder to reflect the correct app name.
Then redeploy using the Azure portal or change it in your GIT and push again.
Based on your 500 error, I think some python packages are not installed correctly.
To check your code is working correctly in naive manner, do as follows.
If you are developing on Windows machine, copy all of your site-packages files in development machine to WebApp /site/wwwroot/env/Lib/site-packages folder.
Hit Restart in Azure Portal and F5 in browser.
If it works, your deployment process might have a problem. Mainly it is caused by library installation.
First, check you have requirements.txt at the root folder. This documentation describes some considerations to load Flask on Azure WebApp. Of course, it would be really helpful to read the documentation from the first line carefully.
Second, login WebApp via FTP and check the package is installed correctly. You can see /pip folder has pip.log file, and /site/wwwroot/env/Lib/site-packages folder has its libraries.
For some libraries which you might require more than simple hello world app, you may have to push x86 .whl files along with python codes as they are not installed correctly in x86 environment.
Additionally, in order to show internal error to outside, consider to apply this option during development (not for production).

How to change and reload python code in waitress without restarting the server?

I am using waitress to serve the web application content like.
waitress-serve --port=8000 myapp:application
While developing, as I change code, I continuously had to restart the waitress-serve to see my changes. Is there a standard way I can automate this?
I know this is an old question but I had a similar issue trying to enable hot reload functionality for my REST API using Falcon framework.
Waitress does not monitor file changes but you could use hupper on top of it. Pretty simple:
$ pip install hupper
$ hupper -m waitress --port=8000 myapp:application
It works on Windows too.
Based on the comment by #Dirk, I found the archive.org link to the snippet mentioned. I was able to get Waitress reloading by using Werkseug directly. Using Werkzeug's decorator run_with_reloader causes the app to restart whenever a Python file changes. (Werkzeug is used within Flask so it should be available).
Additionally, the app.debug = True line causes Flask to reload template files when they change. So you may want both considering your particular situation.
import werkzeug.serving
#werkzeug.serving.run_with_reloader
def run_server():
app.debug = True
waitress.serve(app, listen='127.0.0.1:4432')
if __name__ == '__main__':
run_server()
Once I had my server set up this way, it auto-reloaded the server whenever any file changed.

Installing bottlepy and running python script

I am trying to run this simple python script through my terminal window:(it is the example on the bottlepy website)
from bottle import route, run, template
#route('/hello/:name')
def index(name='World'):
return template('<b>Hello {{name}}</b>!', name=name)
run(host='localhost', port=8080)
In order to do so, I installed bottlepy from Terminal using
easy_install bottle
which instals it in /Library/Frameworks/Python.framework/Versions/4.2.30201/bin
When I try to run my python document in my terminal window:
cd ~/Dropbox/Work/MongoDB/
python hello.py
It returns:
ImportError: No module named bottle
I'm not sure what to do to put bottlepy in the correct emplacement so that it works. I'm sure it must be something silly but I can't think of what to do.
Thank you!!
xx
Take a look at the docs. You can download bottle.py directly to the folder with the rest of the progs, or install in the way shown.
In order to run the file mostly you need to create app first:
app = bottle.Bottle()
And later run it or serve by external web server like waitress

Categories

Resources