In my local host, I'm unable to see any message. I am using jupyter notebook.
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World"
if __name__ == "__main__":
app.debug = True
app.run(host='0.0.0.0',port=5005)
Related
How to deploy/host python flask rest API on the local windows system?
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World’
if __name__ == '__main__':
app.run()
I'm attempting to deploy a simple web app, and I'm using command line waitress-serve --call command. But every time, the command immediately returns 1. Malformed application 'name_of_project_here'.
Here's my flask web app in python:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def index():
return render_template('base.html')
if __name__ == '__main__':
app.run(debug = True)
and the command I run is just
waitress-serve --call "name_of_project"
I did try looking through the documentation, and I found where the error occurs, but couldn't find an explanation of why it's occurring. What does malformed application mean?
Minimal working example:
If you put code in file main.py
from flask import Flask
def create_app():
app = Flask(__name__)
#app.route('/')
def index():
return "Hello World!"
return app
if __name__ == '__main__':
app = create_app()
app.run()
then you can run it as
waitress-serve --call "main:create_app"
So "name_of_project" has to be "filename:function_name" which creates Flask() instance.
It can't be any text. And if you forget : then you may see "Malformed application"
when defining your appname and the function that initialize your app with : dont put them into quotes('')
I have just setup my first Flask project hosted on a server (a2hosting). Now, I want to run on the web application my python script (calculating a few things and then redirects to other url).
But I am not sure how do I set it up on my app server in a2hosting?
# A very simple Flask Hello World app for you to get started with...
from flask import Flask ,redirect
app = Flask(__name__)
#app.route('/set/<hana>')
def hello_world(hana):
import googlemaps
name = hana
return redirect('http://mycamp.co.il/yesh/'+name , code=302)
#app.route('/hello')
def hiii():
import pymongo
def insert(myreq):
dd={
"hihi":"yes1yes"
}
myreq.insert_one(dd)
return True
client = pymongo.MongoClient("mongodb://DETAILES.#routeplan-shard-00-00-d416l.gcp.mongodb.net:27017,routeplan-shard-00-01-d416l.gcp.mongodb.net:27017,routeplan-shard-00-02-d416l.gcp.mongodb.net:27017/test?ssl=true&replicaSet=routeplan-shard-0&authSource=admin&retryWrites=true")
mydb = client['router']
myplan = mydb['planner']
mydis = mydb['distance']
myopt = mydb['optimal']
myreq = mydb['request']
myus = mydb['user']
q=insert(myreq)
return 'helo world %s' + q
if __name__ == '__main__':
app.run(debug = True)
I got this very simple Python code, but somehow I always get an error:
IndentationError: expected an indented block
I checked all tabs and spaces twice and I can not find the mistake.
Can someone please give me a hint?
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!\n'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
May be it is due the editor or improper indentation.
Your code is perfectly running in my pc but I would suggest to run the below code
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.debug = True
app.run(port=8080)
For me worked this in the docker example.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0')
I want to use the Flask as my RESTful API server, but the main thread hangs and it doesn't execute the code after the app.run().
In util/restAPI.py
from flask import Flask
app = Flask('__name__')
#app.route('/')
def index():
return "Hello, World!"
In main.py
from util import restAPI
if __name__ == "__main__":
restAPI.app.run()
print "haha"
Should I use threads or something else to help me?