In [1]:
!pip3 install Flask
In [ ]:
from flask import Flask
app = Flask(__name__)
@app.route("/hello") #specify the path following called "/
def hello():
return "Hello, world"
app.run()
In [ ]:
!python hello.py
In [ ]:
localhost:5000/greeting?to
In [ ]:
import random
from flask import Flask, request, render_template
app=Flask(_name_)
greets=["hello","hi","salutations","howdy","sup", "hey"]
places=["region", "continent", "world","solar system", "galaxy","local cluster", "universe"]
@app.route("/greeting") #speficy the host to go the /greeting route
def greet_generator():
x=random.choice(greets)
y=random.choice(places)
return render_template("greeting.html",
greet=x
place=y)
app.run()
an html form is a bit of a html code that
In [ ]:
from flask import Flask, request, render_template
app=Flask(_name_)
@app.route("/")
def display_form():
return render_template("simplify_home.html")
@app.route("/transformed", methods=["POST"]) #methods=["POST"]:to make a post request
def display_transformation():
return"put transformed text here"
app.run()
In [ ]:
@app.route("/transformed")
def display_tranformation():
our_text = request.form['text']
words = [w for w in our_text.split() if len(w) <=5]
out_text=''.join(words)
return render_template("simply_transformed.html",
output=output_text)
app.run()
curl http://localhost:5000/countries?lookup_name=France france, paris, 64933400 lookup_name=United+States
In [ ]:
expected output:
[
{'name':'Albania', 'capital':'Tirane',
'population':7000000},
{'name':'asdf', 'capital':'asdfasdf', 'population':12345 },
...
]
In [ ]:
from flask import Flask, request,jsonify#take a dictionary and converts it to json
import pg8000
app=Flask(_name_)
conn=pg800.connect(database="mondial")
@app.route("/countries")
def get_countries():
popgt=request.args.get('population_gt',0)
cursor=conn.cursor()
cursor.execute(
"SELECT name, capital, population FROM country WHERE population>=%s ORDER BY name"",[popgt])
In [ ]:
from flask import Flask, request,jsonify#take a dictionary and converts it to json
import pg8000
app=Flask(_name_)
conn=pg800.connect(database="mondial")
@app.route("/countries")
def country_info():
cinfo=None
cname=request.args.get('lookup_name', None)#check if the lookup_name key is in the dictionary, if it isn't,
#perform a database seach
if cname:
cursor=conn.cursor()
cursor.execute(
"SELECT name, capital, population FROM country WHERE name=%s",
[cname]) # query string
response=cursor.fetchone()
if response:
cinfo={
'name':response[0],
'capital':response[1],
'population':int(response[2])
}
#format the results as text(html)
#output=response[0] + ", "+ response[1]+", "+str(response[2])
#return that text
#return output
return render_template("country_data.html",
country=cinfo)
app.run()
In [ ]:
lunch={'sandwich':1, 'chips':1, 'soda':2}
In [ ]:
lunch['sandwich']
In [ ]:
carrot_count=0
if 'carrots' in lunch:
carrot_count=lunch['carrots']
print(carrot_count)
In [ ]:
lunch.get('sandwich')
<form action="/countries" method="GET">
<input type='text' name="lookup_name">
<input type="submit">
</form>
<body>
In [ ]:
In [ ]: