I want to display the variable value using flask,
Here is the code which i have tried
from flask import Flask
app = Flask(__name__)
#app.route("/")
def get_values():
v1 = 1
return "v1"
if __name__ == "__main__":
app.run()
I need to display the value of v1 that is 1 in the application.
If you just want to serve a plain text response containing the string representation of the number in the v1 variable, all you need is:
return str(v1)
Related
How can I take input from url in flask from the parameter?
#app.route('/<id>')
def give_id(id):
return id
I need to take the above Id from input and pass it to other function without again needing to write "id"
def some_function():
variable1 = vid_stream.get_hls_url(give_id("I need to again pass that Id here"))
How can I directly use the id from give_id(id) function and feed it into vid_stream.get_hls_url function?
Posting complete demo code, In case someone needs to run locally.
from flask import Flask
app = Flask(__name__)
#app.route('/<id>')
def give_id(id):
return id
def some_function():
variable1 = vid_stream.get_hls_url(give_id("I need to again pass that Id here"))
print(variable1)
some_function()
if __name__ == '__main__':
app.run(host='0.0.0.0',port=8080)
This should work. id is reserved keyword so i have renamed it you can call the function by passing the value. You can call some_function from you give_id view directly.
from flask import Flask
app = Flask(__name__)
def some_function(p_id):
variable1 = vid_stream.get_hls_url(give_id("I need to again pass that Id here"))
print(variable1)
return variable1
#app.route('/<id>')
def give_id(p_id):
output = some_function(p_id)
return p_id
if __name__ == '__main__':
app.run(host='0.0.0.0',port=8080)
import flask
import sentimentanalyzer
app: object = flask
#app.route("/")
def default() :
return "Hello world>>!!"``
#app.route("/predictsentiment/<query>",method=['GET'])
def predict(query):
sentiment = sentimentanalyzer.returnSentiment(query)
return flask.jsonify(sentiment)
if __name__ == "__main__":
app.run()
You need to import Flask class from flask module.
from flask import Flask
import sentimentanalyzer
app = Flask(__name__)
#app.route("/")
def default() :
return "Hello world>>!!"
#app.route("/predictsentiment/<query>",methods=['GET'])
def predict(query):
sentiment = sentimentanalyzer.returnSentiment(query)
return flask.jsonify(sentiment)
if __name__ == '__main__':
app.run()
I see you have not found a solution yet.
from flask import Flask
import sentimentanalyzer
app = Flask(__name__)
#app.route("/")
def default() :
return "Hello world>>!!"
#app.route("/predictsentiment/<query>",methods=['GET'])
def predict(query):
sentiment = sentimentanalyzer.returnSentiment(query)
return flask.jsonify(sentiment)
if __name__ == '__main__':
app.run()
Though the above code is correct, your error may be the initialization or the structure of the app. Please post the structure.
init.py needs to be put in a directory and the directory is called.
Try renaming your init.py to app.py, should solve the error.
As a reference the Quickstart docs here: https://flask.palletsprojects.com/en/1.1.x/quickstart/
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'
You need to create an object from the Flask class to get started.
And methods needs to be plural. (ref: https://flask.palletsprojects.com/en/1.1.x/quickstart/#http-methods)
Not relevant tot the question the default route has invalid syntax with the double backticks resulting in:
File "/home/tom/tmp/test.py", line 8
return "Hello world>>!!"``
^
SyntaxError: invalid syntax
Regards,
Tom
You need to import the flask class from the flask module. Check how to make minimal application here
import flask
import sentimentanalyzer
app: flask.Flask = flask.Flask(__name__)
#app.route("/")
def default() :
return "Hello world>>!!"
#app.route("/predictsentiment/<query>")
def predict(query):
sentiment = sentimentanalyzer.returnSentiment(query)
return flask.jsonify(sentiment)
if _name_== " main ":
app.run()
Hear is my code:
from flask import Flask
from flask import Markup
from flask import Flask
from flask import render_template
app = Flask(__name__)
#app.route("/")
def chart():
labels = ["2009-Q1","2009-Q2","2009-Q3","2009-Q4","2009-Q1","2009-Q2","2009-Q3","2009-Q4","2009-Q1","2009-Q2","2009-Q3","2009-Q4"]
values = [9,6,6,10,9,7,5,4,10,6,10,8]
return render_template('chart.html', values=values, labels=labels)
#app.route("/chart")
def chart():
labels = ["2009-Q1","2009-Q2","2009-Q3","2009-Q4","2009-Q1","2009-Q2","2009-Q3","2009-Q4","2009-Q1","2009-Q2","2009-Q3","2009-Q4"]
values = [9,6,6,10,9,7,5,4,10,6,10,8]
return render_template('chart.html', values=values, labels=labels)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5001)
The first #app.route("/") works correctly the second does not disappear any Chart like the first one. it disappears just some text of HTML. Any one can help?
First, you don't need to import Flask twice so you can delete one line of
from flask import Flask
The reason it's not working is because you defined two of the same functions 'chart()'. There also seems to be something wrong with the name 'chart' as a route, if you change that, it should be working.
I have two Python scripts:
tnk_flask.py: This creates a flask webserver and two pages.
pitoka.py: this creates a random number.
My goal is the following:
When I run pitonka.py, I always generate a random number which I'd like to pass to tnb_flask.py, but, after refreshing those pages nothing changes.
What can be the mistake?
tnb_flask.py:
from flask import Flask # ezzel importáljuk ki
from pitonka import *
app = Flask(__name__)
#app.route('/')
def index():
denis = alma()
return str(denis)
#app.route('/tuna')
def index__():
return str(numbi)
if __name__ == "__main__":
app.run(debug=True)
pitonka.py:
from flask import Flask
# import tnb_flask
import random
numbi = random.randint(10, 100)
print(numbi)
def alma():
return numbi + 17
In pitonka.py you are assigning the random number to a variable. So it will only get the random variable once. Instead return the random number in the function.
def alma():
return random.randint(10, 100) + 17
You could serve static pages via flask and re-render them from templates in pitonka.py
I did everything according to the flask documentation, but I always get the error 404. I really don't get what I am doing wrong. Perhaps, someone can look at my code and give me a push in the direction of my mistake? It would be too generous, I am really stuck here for hours now and I am almost at the point to give up again. It is so frustrating.
my app:
import flask
app = flask.Flask(__name__)
def main():
register_blueprints()
app.run(debug=True)
def register_blueprints():
from pypi_org.views import home_views
from pypi_org.views import package_views
app.register_blueprint(package_views.blueprint)
app.register_blueprint(home_views.blueprint)
if __name__ == '__main__':
main()
my home_views.py file
import flask
import pypi_org.services.package_service as package_service
blueprint = flask.Blueprint('home', __name__, template_folder='templates')
#blueprint.route('/')
def index():
test_packages = package_service.get_latest_packages()
return flask.render_template('home/index.html', packages=test_packages)
#blueprint.route('/about')
def about():
return flask.render_template('home/about.html')
I found the answer to this in the 'final' github files provided for the course (see the link provided by OP), there's an addition of a couple lines in app.py:
if __name__ == '__main__':
main()
else:
register_blueprints()
Perhaps simplify app.py:
import flask
from pypi_org.views import home_views
from pypi_org.views import package_views
app = flask.Flask(__name__)
app.register_blueprint(package_views.blueprint)
app.register_blueprint(home_views.blueprint)
if __name__ == '__main__':
print (app.url_map)
app.run(debug=True)
Note the addition of the second last line prints all the URL routes to the terminal when you run the application, which helps for debugging.