Skip to main content

Incoming!

 Incoming!

Today, we're going to learn how to deal with data from forms in Flask.


πŸ‘‰ To start, I've added yesterday's HTML code for my form in main.py for you already. (You're welcome!) Go take a look!


πŸ‘‰ However, at the moment, the app.route() has no method associated with it, so I need to create a route for this page to receive the data.


First, I need a new import: request.


Then I create the app.route - I also need to add an extra argument to specify the methods being received. At the moment, that's just 'post', but it does need to be ALL CAPS - POST.


Finally I define the process() subroutine that returns request.form


πŸ‘‰ Here's the new code on its own:


from Flask import Flask, request

app.route('/process', methods=["POST"])

def process():

  return request.form



πŸ‘‰ And here it is as part of the whole code:


from flask import Flask, request

app = Flask(__name__)

app.route("/process", methods=["POST"])

def process():

  return request.form

  

@app.route('/')

def index():

  page = """<form method = "post" action="/process">

    <p>Name: <input type="text" name="username" required> </p>

    <p>Email: <input type="Email" name="email"> </p>

    <p>Website: <input type="url" name="website"> </p>

    <p>Age: <input type="number" name="age"> </p>

    <input type="hidden" name="userID" value="232"> </p>

    <p>

      Fave Baldy: 

      <select name="baldies">

        <option>David</option>

        <option>Jean Luc Picard</option>

        <option>Yul Brynner</option>

      </select>

    </p>

    <button type="submit">Save Data</button>

  </form>

    

    

    """

  return page

app.run(host='0.0.0.0', port=81)



This will get us the data from the form in a dictionary format, like this:  

 






Comments

Popular posts from this blog

Hide & Remove,Come Back!,

 Hide & Remove DISCLAIMER: I promise the good stuff is coming back. We have to go through the valley to get to the mountain, right? Sometimes, we want to remove a button, image or piece of text from the screen. To do this, we use pack_forget(). πŸ‘‰ We'll start with our default tkinter program. import tkinter as tk window = tk.Tk() window.title("Hello World")  window.geometry("300x200")  hello = tk.Label(text = "Hello World")  hello.pack()  button = tk.Button(text = "Click me!")  button.pack() tk.mainloop() πŸ‘‰ Now I'm going to add a new subroutine to hide the label and call it on a button click. import tkinter as tk window = tk.Tk() window.title("Hello World")  window.geometry("300x200")  # New subroutine def hideLabel():   hello.pack_forget() # Removes the 'hello' label hello = tk.Label(text = "Hello World")  hello.pack()  button = tk.Button(text = "Click me!", command = hideLabel) # Call...

String Manipulation

   String Manipulation Does this code look familiar from the insult generator project? name = input("What's your name? ") if name == "David" or name == "david":   print("Hello Baldy!") else:    print("What a beautiful head of hair!") Right now, if the user writes "DAVID" or "david", the if statement works correctly. However, "DaVID" does not give the correct output. To the computer, " david", "dAviD", and "david" are completely different. To simplify what the user typed in, we can add these functions to the end of the name of the variable: .lower() = all letters are lower case .upper() = all letters are upper case .title() = capital letter for the first letter of every word .capitalize() = capital letter for the first letter of only the first word    

Web Scraping

 Web Scraping Some websites don't have lovely APIs for us to interface with. If we want data from these pages, we have to use a tecnique called scraping. This means downloading the whole webpage and poking at it until we can find the information we want. You're going to use scraping to get the top ten restaurants near you. Get started πŸ‘‰ Go to a website like Yelp and search for the top 10 reastaurants in your location. Copy the URL.   url = "https://www.yelp.co.uk/search?find_desc=Restaurants&find_loc=San+Francisco%2C+CA%2C+United+States"   Import libraries πŸ‘‰ Import your libraries. Beautiful soup is a specialist library for extracting the contents of HTML and helping us parse them. Run the Repl once your imports are sorted because we want the Beautiful Soup library to be installed (it'll run quicker this way). import requests from bs4 import BeautifulSoup url = "https://www.yelp.co.uk/search?find_desc=Restaurants&find_loc=San+Francisco%2C+CA%2C+Unite...