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.
Related
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 googled this and tried everything but I could not run my test.py on apache.
I have updated httpd.conf file to AddHandler .py
#!/usr/local/opt/python/bin/python3.7
print("Hello World!")
When I open this file in browser I am getting this error
Error message:
End of script output before headers: test.py
Have you tried to use Flask instead? It start a server that you can access via browser.
Here you can find an Hello world tutorial, or also here.
First, you have to install Flask: pip install Flask.
Then the server code, saved in a python file, for example your_server.py, has to look like this:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
You can run the script, then go to http://localhost:5000/ and you'll see the 'Hello World!' message.
Now you can modify the method hello() to do what you need, or create other method/operations.
The tutorial can give you more details, but this is the main concept.
This question already has answers here:
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Closed 4 years ago.
I'm running my flask web app on local from command line using python __init__.py command. I want to run/deploy flask web without using any command. Like spring web app.
init.py
from flask import Flask, url_for
from flask import jsonify
from werkzeug.serving import run_simple
from werkzeug.wsgi import DispatcherMiddleware
import db_config
app = Flask(__name__)
app.config['APPLICATION_ROOT'] = '/my-web-app'
def simple(env, resp):
resp(b'200 OK', [(b'Content-Type', b'text/plain')])
return [b'Hello my app User']
#app.route("/")
def hello():
return "Hello world"
#app.route('/test', methods=['GET'])
def get_tasks():
db = db_config.getconnection();
cur = db.cursor()
cur.execute("SELECT * FROM emp")
items = cur.fetchall()
print(items)
db.commit()
cur.close()
db.close()
return jsonify({'tasks': items})
app.wsgi_app = DispatcherMiddleware(simple, {'/my-web-app': app.wsgi_app})
if __name__ == "__main__":
app.run()
Is there any way to create .WAR file of flask web app and deploy it on server? And start web app when we give request.
Is there any way to create a Java .WAR file from a Flask app? Possibly, but I really wouldn't recommend it. Python is not Java.
Is there any way to deploy a Flask application on a server? Oh yes, lots of ways. The Deployment Options page in the Flask documentation lists several options, including:
Hosted options: Deploy Flask on Heroku, OpenShift, Webfaction, Google App Engine, AWS Elastic Beanstalk, Azure, PythonAnywhere
Self-hosted options: Run your own server using a standalone WSGI container (Gunicorn, uWSGI, Gevent, Twisted), using Apache's mod_wsgi, using FastCGI, or using old-school vanilla CGI.
Of the self-hosted options, I would definitely recommend nginx rather than Apache as a front-end web server, and nginx has built-in support for the uWSGI protocol, which you can then use to run your app using the uwsgi command.
But I would definitely recommend looking into one of the hosted options rather than being in the business of running your own web server, especially if you're just starting out. Google App Engine and AWS Elastic Beanstalk are both good platforms to start out with as a beginner in running a cloud web application.
based on this structure: http://flask.pocoo.org/docs/patterns/packages/
I also tried this post: Deploying Flask app to Heroku
I am having trouble getting this to work on heroku. I usually get the PORT does not set within 60 seconds error. I have read other SO posts and just can't figure out if my project structure is wrong or my procfile. I tried other ports than 5000 as well.
Here is my current project structure:
/myapplication
Procfile
runserver.py
/applicationfolder
__init__.py
views.py
Here is my Procfile
web: python runserver.py $PORT
Here is my runserver.py
from applicationfolder import app
app.run()
if __name__ == '__main__':
import os
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
Here is my init.py
import os
from flask import Flask
from flask import render_template, jsonify, request
app = Flask(__name__)
app.config.from_object('config')
import applicationfolder.views
From there views.py runs.
This works locally with foreman start and python runserver.py, but does not work with heroku. I have tried many things with PORT but port doesn't seem to set even with a different PORT than 5000. I think it has something to do with my project structure.
The app.run() was in there twice, which as you noted is what's screwing things up. The app.run() invokes a simply pure-python development server so that you can easily run and/or debug your script.
By invoking it at the module level (right under your import in runserver.py), you were effectively trying to start the development server as the python code was loaded, and then when it went to run it when invoked from the Procfile, the development server was already in flight, having been starting with it's defaults (latest version of Flask is pulling relevant defaults from the SERVER_NAME environment variable). By having it in both places, you were trying to invoke that method twice.
You basically want either the straight up module load (in which case, kill off the code under "if name ...", or you use the code when invoking under main, in which case don't start the service at module load time.
Twilio newbie question:
I created an app that uses the Twilio API as I followed along to the tutorial by General Assembly
The files I added are
app.py
Procfile
requirements.txt
app.py
from flask import Flask
from flask import request
from twilio import twiml
import os
app = Flask(__name__)
#app.route('/caller', methods=['POST'])
def caller():
response = twiml.Response()
response.enqueue("Christmas Queue")
return str(response)
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
app.debug = True
app.run=(host='0.0.0.0'. port=port)
Procfile
web: python app.py
requirements.txt
flask>=0.9
twilio>=3.1
I deployed the app to Heroku. And then I added the URL to Twilio
I called to test it, but got an error. Not sure what my next steps can be to troubleshoot this further.
Your first task should be to run heroku logs -t and actually look at the Heroku output when your app is deployed.
You have a few syntax errors:
app.run=(host='0.0.0.0'. port=port)
^ ^
Remove that equals sign, replace the period with a comma and your script will run.
I would read through Heroku's Python tutorial as well.