I am facing problem while deploying flask application on bluehost server.
I made a newdomain.maindomain.com subdomain, and later did the following:
SSHed to the "newdomain" folder
created virtualenv and installed flask
created a file called "passenger_wsgi.py"
import sys,os
INTERP = os.path.join(os.environ['HOME'],'medsathome.freedatametric.com','bin','python')
if sys.executable != INTERP:
os.execl(INTERP,INTERP,*sys.argv)
sys.path.append(os.getcwd())
from flask import Flask
application = Flask(__name__)
#application.route('/')
def index():
return 'Hello Passenger'
now when I go to the link, I dont see Hello Passenger being printed on screen.
What is wrong?
Related
Starting my Flask app using:
flask run
Doesn't appear to work... and I get the error message:
Error: While importing 'entry', an ImportError was raised.
however if I run:
python entry.py
The app will build successfully? Why is this? Both FLASK_APP and FLASK_ENV are set correctly, here is my folder structure:
entry.py:
from application import create_app
app = create_app()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
application/init.py:
import os
from flask import Flask
from config import DevConfig, TestConfig, ProdConfig
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
from application.models import Report
def create_app(testing=False):
app = Flask(__name__)
flask_env = os.getenv("FLASK_ENV", None)
# Configure the application, depending on the environment
if testing:
app.config.from_object(TestConfig)
elif flask_env == "development":
app.config.from_object(DevConfig)
elif flask_env == "production":
app.config.from_object(ProdConfig)
else:
raise ValueError("FLASK_ENV is not set or there is an unknown environment type!")
# init plugins, if any
db.init_app(app)
if flask_env == "development":
with app.app_context():
db.create_all()
db.session.commit()
# register blueprints
register_blueprints(app)
return app
def register_blueprints(app):
from application.main import main_blueprint
app.register_blueprint(main_blueprint)
If you want to start the application through the flask executable, then you have to consider that flask will look for the app.py file containing the app application, if not (as in your case), then you will have to correctly set the value of the environment variable FLASK_APP, which will be equal to FLASK_APP=entry.py:app
On Linux, macOS:
$ export FLASK_APP=entry.py:app
$ flask run
On Windows:
$ set FLASK_APP=entry.py:app
$ flask run
Take a look here: Run The Application.
Here, however, it is explained how flask looks for the application to run
In this case (python entry.py) everything works correctly, because flask is invoked via python inside the main section, which instead is not called if entry.py is executed directly from flask, in fact flask will not enter the main section, but will look for the app.py file and the app variable inside it. (Obviously it will look for entry.py and app if you have configured the environment variable correctly)
If I understand the documentation correctly, in Python3 Flask ___init___.py is used for importing classes and can be completely empty.
I am trying to use app.py to hold my code, but I don't know how to get Flask to execute app.py. Code I put in __init__.py will execute, but I don't think I should do it that way. I have seen some posts where doing this could cause code to execute when it was just importing.
How do I get Flask to hit my app.py instead of __init__.py?
Here is my wsgi config file
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
from FlaskApp import app as application
application.secret_key = 'something secret'
In my wsgi.py I just import the app (from app.py) and run it. I do have an older setup though but the approach should still apply.
from app import app
if __name__ == "__main__":
app.run()
and my app.py has the global Flask app variable along with all the other setup and Flask code (logging, routes, etc.)
app = Flask(__name__)
Did you set the FLASK_APP environment variable to your app.py file?
$ export FLASK_APP=app
$ flask run
See: https://flask.palletsprojects.com/en/1.1.x/cli/#application-discovery
I'm trying to make an project with flask, but somehow, even if i installed it, i just cannot use flask!
i've tried to change the from Flask import Flask, render_template, request command by just import Flask, but nothing happen, could anyone help, please? Here is the code, if necessary:
import Flask
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')
It's
from flask import Flask
# 1st lowercase
The import is from flask import Flask.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')
Are you sure that you used the same Python version to install flask as you did to run your app? I once installed flask for Python 2.6, but executed the script in Python 3, which also led to the fact that the flask could not be imported. Be sure that the versions are correct.
sudo pip install Flask
sudo pip3 install Flask
I have developed a Machine learning model in anaconda prompt using Python3 so to deploy it and to run it anywhere dynamically on GitHub or as a webservice what is the way?
I have already used Pickle command but it didn't work for me.
import pickle
file_name = "cancerdetection.ipynb"
It didn't deploy my model as webservice.
You can try using flask. It uses python and deploys as a web application. Here's some sample code you can try.
from flask import Flask, render_template, request
app = Flask(__name__, static_url_path='/static')
#app.route('/')
def upload_file():
return "hello world"
if __name__ == '__main__':
app.run(host="0.0.0.0")
By setting
host="0.0.0.0"
you're deploying your application on the net by using your local machine as a server.
I've got a website written in bottle and I'd like to deploy it via Amazon's Elastic Beanstalk. I followed the tutorial for deploying flask which I hoped would be similar.
I tried to adapt the instructions to bottle by making the requirements.txt this:
bottle==0.11.6
and replaced the basic flask version of the application.py file with this:
from bottle import route, run
#route('/')
def hello():
return "Hello World!"
run(host='0.0.0.0', debug=True)
I updated to this version as instructed in the tutorial, and when I wrote eb status it says it's green, but when I go to the URL nothing loads. It just hangs there. I tried the run() method at the end as it is shown above and also how it is written in the bottle hello world application (ie run(host='localhost', port=8080, debug=True)) and neither seemed to work. I also tried both #route('/hello') as well as the #route('/').
I went and did it with flask instead (ie exactly like the Amazon tutorial says) and it worked fine. Does that mean I can't use bottle with elastic beanstalk? Or is there something I can do to make it work?
Thanks a lot,
Alex
EDIT:
With aychedee's help, I eventually got it to work using the following application file:
from bottle import route, run, default_app
application = default_app()
#route('/')
def hello():
return "Hello bottle World!"
if __name__ == '__main__':
application.run(host='0.0.0.0', debug=True)
Is it possible that the WSGI server is looking for application variable inside application.py? That's how I understand it works for Flask.
application = bottle.default_app()
The application variable here is a WSGI application as specified in PEP 333. It's a callable that takes the environment and a start_response function. So the Flask, and Bottle WSGI application have exactly the same interface.
Possibly... But then I'm confused as to why you need that and the call to run.