Flask Flashcards
What is Flask?
It is a server technology. It is a web framework. Django is a framework.
What is a virtual environment?
A folder where you can place all of your information and can hold all projects for that instance. It’s going to store all the software for me
A virtual environment is a clean python environment
When I install Flask why are there other applications that need to be included?
Needs them, dependencies
What is a framework?
It’s a standard structure for how to implement a software project. Contains both the tools needed to do repetitive tasks and then may generate needed project files as well
What is a microframework?
Like a framework but for a minimalistic web application. Less complex than a full stack framework
localhost: 5000
It is a localhost What is :5000…it is a port .
It is like a lane of a highway that all of your traffic is go over. you can change if you want to
What are routes?
Important way to interact with website. It is anything after our .com..THAT”S our route.
Multiple pages at each route. Jinja2 renders our templates for us.
@app.route(“/route”)
What is a static file?
To link to static files we can create a new folder called static. In order to keep the static folder organized we can also create subfolders for css, js and images
Organize your static folder
Review: What is a primitive data type?
Integers, strings, and floats
Review: What is a data structure?
List, dictionary, tuple
Review: What is OOP?
Create objects based on classes and assign attributes and methods to them .
Flask, also what type of framework is it?
Simplicity to build a server, it’s simplicity is why it’s good to learn first as a framework
Flask is a micro framework
What is a template
Something generated in Flask that you can send back as a simple HTML document
What are the key topics you should be familiar with regardless of technology?
Routing,
Clint server cycle - simplified to a degree in Flask. Understanding how it works now will help…which action triggers which events, and what happens when a client makes a request.
Database connection - Every app must connect to a database
What are some concepts we should always focus on when learning flask?
Rendering templates, redirecting, form data, get & post requests, session
If we want to use flask or any pip module, what do we want to use?
A virtualenv
What is a python virtual environment?
Simply put, virtual environments are a tool for creating unique sandboxes to control project dependencies. Each of these virtual environments can contain different versions of Python, Flash, or any combination of packages.
Consider a scenario in which you must put work into 4 projects, each with varying dependencies:
Project 0 – Using Python 2.7.13, Flask 0.10
Project 1 - Using Python 2.7.13, Flask 0.12
Project 2 - Using Python 3.0, Flask 0.10
Project 3 - Using Python 3.4.3, No Flask
This scenario is an excellent use case for the virtualenv Python module. We can create Python environments that are segregated from our system-wide Python installation, with each virtual environment being created specifically for our three projects.
List the steps to creating a virtual environment using flasdk
pip install virtual env (virtual environment for python)
create a folder with a structure to hold your envs
virtualenv flaskEnv (major dependency is flask environment)
source flaskEnv/bin/activate (activate the virtual envrionment)
The (flaskEnv) indicates that your virtualenv is now active. If we install any dependencies they will install into our local flaskEnv folder:
How do I deactivate my flask environement
Just type the deactivate in the command line. Closing your terminal window will also deactivate your virtual environment. pip list shows dependencies
Review: What is pip?
adds packages for python: pip install \_\_\_ pip list = all dependencies pip freeze (requirements file) pip freeze > requirements.txt pip uninstall \_\_\_ pip show Django pip search Flask
Routes are essential part of any web application. A route is much like
A route is much lika . variable name we assign to a request. The job of a route is to communicate to the server what kind of information the client needs. This route name is attached to a rate on our server that points towards a specific
These instructions contain information about how to interpret the data being sent, the operations that need to be completed, and the response that should be sent back.
What is special about templates?
We need to call to a directory called templates
What are the two most common HTTP request methods that fit into web apps
Two most common are GET and POST
What is the GET method?
Any time you visit a website and a page loads in reponse you’re seeing the HTTP GET Method in action! The page is the RESPONSE that have been packaged and returned to your browser in the form of HTML, CSS, and Javascript. So far all of our routes have been for GET requests
What is you were signing into your email and we used a GET method. How horrible would it be if you submitted the login form and your SN and PW were displayed in your URL?
POST to the Rescue! Passes data behind the scenes in the HTTP request message body. These requests are never cached, not saved in your history, can’t be bookmarkeed and aren’t limited to how much data that can be sent. The vast majority of form you’rll be dealing with will use POST requests to send data. In, general forms should ALWAYS be sent using the POST method unless you will need to give users the ability to preserve a query string, as in a Google search.
What about PUT PATCH and DELETE
These exits for the express purpose of designing APIs. These are not supported methods in your HTML code, and only work when being handled by JS AJAX requests
Review: What are forms?
The modern internet is user-driven; much of the actual content of a website is generated by the users of a website. How does the user manage to get his or her data to the page?? One word: forms. The HTML forms are the way in which users are able to pass data to the back end of a website where it can be processed and stored. Processing form data correctly is a huge part of what it takes to become a back-end developer.
Why is the input element so important in forms?
The form tag will set up all of the logistics, but the information you want to gather will be harvested from the elements you place within the form. In our basic example, we have just one type of input, a text field. There are several different input types that can gather data:
Decide on the input elements you want to include in your form. Each must be assigned a name attribute if you want each input to be recognized by your server-side code. When the HTML form gathers up your data it will create a set of key/value pairs with the name attributes as the keys and the data the user enters as the values. Notice in our form above that we give a name to each input element that reflects the information we want to gather.
And that’s the front end! The form data will be sent to the server when the user clicks the submit button. The form data is organized into key/value pairs and submitted to the URL specified by your form’s ‘action’ attribute using the HTTP method specified in your form’s ‘method’ attribute.
Redirecting?
In our POST route we used the redirect method to send a new request to the ‘/’ route. We can redirect to any route that we have defined:
Flask templates (using Jinja2)…how would you do a for loop?
{% for x in range (0,5) %}
<p> {{ x }} </p>
{% endfor %}
Do the bulk of your logic where?
In your python code and not embedded in your html since it will slow down your server response time
What is session?
Remember, an HTTP request/response cycle, it is stateless. That means that each request/response cycle instance is independent and ignorant of any instances that came before or will come after it….
but how does your retailr know and rememer, who you are (account), what you’ve searched for, the items in i your cart, etc….THROUGH PERSISTENT DATA STORAGE. You can use a databaser or you can also come in the form of writing to a file, and that is what session doess
Its like the user can have a conversation of sorts with the web site where a user makes decision that can e tracked so that the server can respond appropriately to create a better user experience.
Cookies vs databases?
You’ve probably heard of the term cookies before. Frameworks like Flask use cookies to store data like sessions. Flask uses secure hashing of session data to send a packet of information from server to client. This packet is known as a cookie. Once your browser has received this cookie, it writes the information contained in it to a small file on your hard drive.
What are the steps we need to perform for form validation?
1) logic - what data do we want to validate?
2) checking if the data is present
3) making sure the data is in the correct format
4) sending the user to the correct destination whether their data is valid or not
5) alerting the user of their errors (if they exist)
What are the most important validation tools for the if/else statement?
IF/Else with a return of true or false
What are flash messages on the server?
Flask messages are string that exist for one redirect cycle. Similar to Session, you can access flask messages through embedded python tags {{}} & {%%} on the views and display them to the user.
What is the difference between flash and session?
Flash messages only last for one redirect while session stays until it is manually popped. This makes flash messages perfect for validation where we only need to display the error or message temporarily.
How can we import databases and use session variables to log a user in?
Connecting to MySQL, import and exporting a MySQL database, include file, database communication management, login and registration, mySQL injection
What is the terminal command to install mySQL
pip install mySQL-python==1.2.5