Flask #app.route('/hello/<name>') 404 Not Found - flask

from flask import Flask
app = Flask(__name__)
#app.route('/hello/<name>')
def hello_name(name):
return 'Hello %s!' % name
if __name__ == '__main__':
app.run()

Enter http://localhost:5000/hello/<name> route.
here, name parameter is taken value from the route.
i.e. http://localhost:5000/hello/smit and its return Hello smit!

Related

subprocess.Popen line in flask app not running on pythonanywhere while it work on local machine.. any idea why or how to catch error?

subprocess.Popen line (40) in flask app not running on pythonanywhere while it works on local machine.. tried different variation of the subprocess line but no luck. no error shown in error logs in pythonanywhere.
any idea why there is a difference to functionality on pythonanywhere vs local machine and how to fix or how to catch error of subprocess?
enter image description here
from flask import Flask, render_template, request
from flask_mysqldb import MySQL
import yaml
from subprocess import Popen, PIPE
from threading import Thread
import time
import subprocess
app = Flask(__name__)
# config db
db = yaml.full_load(open('db.yaml'))
app.config['MYSQL_HOST'] = 'xxx'
app.config["MYSQL_USER"] = "xxx"
app.config["MYSQL_PASSWORD"] = "xxx"
app.config["MYSQL_DB"] = "xxx"
mysql = MySQL(app)
#app.route("/", methods=["GET", "POST"])
def index():
if request.method =="POST":
#fetch form data
userDetails=request.form
title=userDetails["title"]
location=userDetails["location"]
radius=userDetails["radius"]
email=userDetails["email"]
cur = mysql.connection.cursor()
cur.execute("INSERT INTO requests(title, location, radius, email) VALUES(%s, %s, %s, %s)", (title, location, radius, email))
mysql.connection.commit()
cur.close()
subprocess.Popen(['python', 'file.py'])
return render_template('donenew.html')
return render_template("indexnew.html")
if __name__ == "__main__":
app.run(debug=True)

how to add app object in flask's main file

I want to add app object once in main.py which can be used everywhere, but route does not work in this way. What is the issue here?
main.py
from flask import Flask
app = Flask(__name__)
if __name__ == "__main__":
app.run(debug=True)
routes.py
from main import app
#app.route("/", methods = ["GET"])
def home():
return "hi"
However, if declare app = Flask(name) in routes.py and import app in main.py it is working all fine. Working scenario.
main.py
from routes import app
if __name__ == "__main__":
app.run(debug=True)
routes.py
from flask import Flask, jsonify, request
app = Flask(__name__)
#app.route("/", methods = ["GET"])
def home():
return "hi"
my objective is to define app in main.py and import it in other files, but getting issues.
main.py is not even aware that routes.py exists. Import your routes.py file after initializing your app.
# main.py
from flask import Flask
app = Flask(__name__)
import routes # needed
if __name__ == "__main__":
app.run(debug=True)
# routes.py
from __main__ import app
#app.route("/")
def home():
return "hi"

How to reference self in flask route method?

For example, this pattern is usually accomplished with globals. How to use attributed?
from flask import Flask
app = Flask(__name__)
app._this_thing = None
#app.route('/')
def hello_world():
self._this_thing = 123
return 'Hello, World!'
You can import the global variable current_app (see doc) and access it in your function like this:
from flask import Flask, current_app
app = Flask(__name__)
app._this_thing = 'Hello world!'
#app.route('/')
def hello_world():
return current_app._this_thing
Saving the this as so.py and starting it like this
$ FLASK_APP=so.py flask run
then returns the expected response:
$ curl http://localhost:5000
Hello world!

Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again

When I run this, 404 error is been shown. Please help me solving this:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "<h1> Hello Puppy! </h1>"
#app.route("/information")
def info():
return "<h1> Puppies are Cute </h1>"
#app.route("/Puppy/<name>")
def puppy(name):
return "<h1> This is a page for {} </h1>".format(name)
if __name__ == '__main__':
app.run()

requests.get does not response in python?

I have the following script which I tried to get response status from request.get(url)
from flask import *
import requests
app = Flask(__name__)
#app.route('/')
def home():
r = requests.get('http://127.0.0.1:5000/welcome')
print r
return render_template('home.html')
#app.route('/welcome')
def wecome():
print "welcome page"
return "welcome to my page"
if __name__=='__main__':
app.run(debug=True)
When I access endpoint http://127.0.0.1:5000/ the browser keeps spinning none stop without any output result in terminal console as I expected even the error message.
However, if I change the requests url to something else externally as below, the response is showing up the status <Response [200]> in terminal and the page is loaded as normal.
from flask import *
import requests
app = Flask(__name__)
#app.route('/')
def home():
r = requests.get('https://api.github.com/emojis')
print r
return render_template('home.html')
#app.route('/welcome')
def wecome():
print "welcome page"
return "welcome to my page"
if __name__=='__main__':
app.run(debug=True)
what's going on with that?
How can I get response from internal url as I need it badly for my current work?