In [1]:
!pip3 install Flask


Collecting Flask
  Downloading Flask-0.11.1-py2.py3-none-any.whl (80kB)
    100% |████████████████████████████████| 81kB 5.3MB/s 
Collecting Werkzeug>=0.7 (from Flask)
  Downloading Werkzeug-0.11.10-py2.py3-none-any.whl (306kB)
    100% |████████████████████████████████| 307kB 2.7MB/s 
Collecting itsdangerous>=0.21 (from Flask)
  Downloading itsdangerous-0.24.tar.gz (46kB)
    100% |████████████████████████████████| 51kB 10.1MB/s 
Collecting Jinja2>=2.4 (from Flask)
  Using cached Jinja2-2.8-py2.py3-none-any.whl
Collecting click>=2.0 (from Flask)
  Downloading click-6.6.tar.gz (283kB)
    100% |████████████████████████████████| 286kB 2.3MB/s 
Collecting MarkupSafe (from Jinja2>=2.4->Flask)
Building wheels for collected packages: itsdangerous, click
  Running setup.py bdist_wheel for itsdangerous ... - done
  Stored in directory: /Users/sz2472/Library/Caches/pip/wheels/fc/a8/66/24d655233c757e178d45dea2de22a04c6d92766abfb741129a
  Running setup.py bdist_wheel for click ... - done
  Stored in directory: /Users/sz2472/Library/Caches/pip/wheels/b0/6d/8c/cf5ca1146e48bc7914748bfb1dbf3a40a440b8b4f4f0d952dd
Successfully built itsdangerous click
Installing collected packages: Werkzeug, itsdangerous, MarkupSafe, Jinja2, click, Flask
Successfully installed Flask-0.11.1 Jinja2-2.8 MarkupSafe-0.23 Werkzeug-0.11.10 click-6.6 itsdangerous-0.24

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

tag has one attribute called "action", method attribute specify which http methods we use: GET(http method, fetching information from the server)/HTTP


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()

country information server

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')

Country lookup

Country Information

     <form action="/countries" method="GET">
          <input type='text' name="lookup_name">
          <input type="submit">
     </form>
 <body>

In [ ]:


In [ ]: