Web Development With Flask Pdf
Flask Web Development, 2nd Edition by Miguel Grinberg Stay ahead with the world's most comprehensive technology and business learning platform. With Safari, you learn the way you learn best. Get started with Flask, the popular web framework that’s small, lightweight, and powerful. This video tutorial uses use short and simple examples to help beginning Python developers explore the features of Flask and some of its extensions. Torrent2file.com » Ebooks » Flask Web Development: Developing Web Applications with Python, 2nd Edition (True PDF). Flask is commonly used with MongoDB, which gives it more control over databases and history. Applications that use the Flask framework include Pinterest, [6] LinkedIn, [7] and the community web page for Flask itself.
ReviewsAuthor: Miguel Grinberg
Pub Date: 2014
ISBN: 978-1-4493-7261-3
Pages: 258
Language: English
Format: PDF/EPUB
Size: 10 Mb
Download
Take full creative control of your web applications with Flask, the Python-based microframework. With this hands-on book, you’ll learn Flask from the ground up by developing a complete social blogging application step-by-step. Author Miguel Grinberg walks you through the framework’s core functionality, and shows you how to extend applications with advanced web techniques such as database migration and web service communication.
Flask Extensions
Flask is designed to be extended. It intentionally stays out of areas of important functionality such as database and user authentication, giving you the freedom to select the packages that fit your application the best, or to write your own if you so desire.
There is a large variety of extensions for many different purposes that were created by the community, and if that is not enough, any standard Python package or library can be used as well. To give you an idea of how an extension is incorporated into an application, the following section adds an extension to hello.pythat enhances the application with command-line arguments.
Command-Line Options with Flask-Script
Flask’s development web server supports a number of startup configuration options, but the only way to specify them is by passing them as arguments to the app.run()call in the script. This is not very convenient; the ideal way to pass configuration options is through command-line arguments.
Flask-Script is an extension for Flask that adds a command-line parser to your Flask application. It comes packaged with a set of general-purpose options and also supports custom commands.
…
Twitter Bootstrap Integration with Flask-Bootstrap
Bootstrap is an open source framework from Twitter that provides user interface components to create clean and attractive web pages that are compatible with all modern web browsers.
Bootstrap is a client-side framework, so the server is not directly involved with it. All the server needs to do is provide HTML responses that reference Bootstrap’s cascading style sheets (CSS) and JavaScript files and instantiate the desired components through HTML, CSS, and JavaScript code. The ideal place to do all this is in templates.
The obvious way to integrate Bootstrap with the application is to make all the necessary changes to the templates. A simpler approach is to use a Flask extension called FlaskBootstrap to simplify the integration effort. Flask-Bootstrap can be installed with pip:
…
Custom Error Pages
When you enter an invalid route in your browser’s address bar, you get a code 404 error page. The error page is now too plain and unattractive, and it has no consistency with the page that uses Bootstrap.
Flask allows an application to define custom error pages that can be based on templates, like regular routes. The two most common error codes are 404, triggered when the client requests a page or route that is not known, and 500, triggered when there is an unhandled exception. Example 3-6shows how to provide custom handlers for these two errors.
…
Error handlers return a response, like view functions. They also return the numeric status code that corresponds to the error.
The templates referenced in the error handlers need to be written. These templates should follow the same layout of the regular pages, so in this case they will have a navigation bar and a page header that shows the error message.
The straightforward way to write these templates is to copy templates/user.html to templates/404.htmland templates/500.htmland then change the page header element in these two new files to the appropriate error message, but this will generate a lot of duplication.
Jinja2’s template inheritance can help with this. In the same way Flask-Bootstrap provides a base template with the basic layout of the page, the application can define its own base template with a more complete page layout that includes the navigation bar and leaves the page content to be defined in derived templates. Example 3-7 shows templates/base.html, a new template that inherits from bootstrap/base.htmland defines the navigation bar, but is itself a base template to other templates such as templates/user.html, templates/404.html, and templates/500.html.
In this series, we'll be using Python, Flask and MySQL to create a simple web application from scratch. It will be a simple bucket list application where users can register, sign in and create their bucket list.
This tutorial assumes that you have some basic knowledge of the Python programming language. We'll be using Flask, a Python web application framework, to create our application, with MySQL as the back end.
If you need to brush up on your Python skills, try the Introduction to Python course, which gives you a solid foundation in the language for just $5.
Introduction to Python Flask
Flask is a Python framework for creating web applications. From the official site,
Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions.
When we think about Python, the de facto framework that comes to our mind is the Django framework. But from a Python beginner's perspective, Flask is easier to get started with, when compared to Django.
Setting Up Flask
Setting up Flask is pretty simple and quick. With pip package manager, all we need to do is:
Once you're done with installing Flask, create a folder called FlaskApp. Navigate to the FlaskApp folder and create a file called app.py. Import the flask module and create an app using Flask as shown:
Now define the basic route / and its corresponding request handler:
Next, check if the executed file is the main program and run the app:
Save the changes and execute app.py:
Point your browser to http://localhost:5000/ and you should have the welcome message.
Web Development With Flask Pdf Pdf
Creating a Home Page
First, when the application runs we should show a home page with the latest bucket list items added by users. So let's add our home page to our application folder.
Flask looks for template files inside the templates folder. So navigate to the PythonApp folder and create a folder called templates. Inside templates, create a file called index.html. Open up index.html and add the following HTML:
Open up app.py and import render_template, which we'll use to render the template files.
Modify the main method to return the rendered template file.
Save the changes and restart the server. Point your browser to http://localhost:5000/ and you should have the below screen:
Creating a Signup Page
Step 1: Setting Up the Database
We'll be using MySQL as the back end. So log into MySQL from the command line, or if you prefer a GUI like MySQL work bench, you can use that too. First, create a database called BucketList. From the command line:
Enter the required password and when logged in, execute the following command to create the database:
Once the database has been created, create a table called tbl_user as shown:
We'll be using Stored procedures for our Python application to interact with the MySQL database. So, once the table tbl_user has been created, create a stored procedure called sp_createUser to sign up a user.
When creating a stored procedure to create a user in the tbl_user table, first we need to check if a user with the same username already exists. If it exists we need to throw an error to the user, otherwise we'll create the user in the user table. Here is how the stored procedure sp_createUser would look:
Step 2: Create a Signup interface
Navigate to the PythonApp/templates directory and create an HTML file called signup.html. Add the following HTML code to signup.html:
Also add the following CSS as signup.css to the static folder inside PythonApp.
In app.py add another method called showSignUp to render the signup page once a request comes to /showSignUp:
Save the changes and restart the server. Click on the Sign Up button on the home page and you should have the signup page as shown:
Step 3: Implement a Signup method
Next, we need a server-side method for the UI to interact with the MySQL database. So navigate to PythonApp and open app.py. Create a new method called signUp and also add a route /signUp. Here is how it looks:
We'll be using jQuery AJAX to post our signup data to the signUp method, so we'll specify the method in the route definition. Maison de m all cg pics.
In order to read the posted values we need to import request from Flask.
Using request we can read the posted values as shown below:
Once the values are read, we'll simply check if they are valid and for the time being let's just return a simple message:
Also import json from Flask, since we are using it in the above code to return json data.
Step 4: Create a Signup request
Web Api Development With Flask Pdf
We'll be using jQuery AJAX to send the signup request to the Python method. Download and place jQuery inside PythonApp/static/js and add a link to it from the signup page. Once jQuery has been included, we'll add a jQuery POST request when the user clicks the Sign Up button.
So, let's attach the signup button click event as shown:
Save all the changes and restart the server. From the Sign Up page, fill in the details and click Sign Up. Check the browser console and you should have the below message:
Step 5: Call the MySQL Stored Procedure
Once we have the name, email address and password, we can simply call the MySQL stored procedure to create the new user.
To connect with MySQL, we'll be using Flask-MySQL, which is a Flask extension. In order to get started with Flask-MySQL, install it using pip package manager:
Import MySQL inside app.py:
Earlier we defined our app as shown:
Along with that include the following MySQL configurations:
First, let's create the MySQL connection:
Miguel Web Development With Flask O'reilly
Once the connection is created, we'll require a cursor to query our stored procedure. So, using conn connection, create a cursor.
Before calling the create user stored procedure, let's make our password salted using a helper provided by Werkzeug. Import the module into app.py:
Use the salting module to create the hashed password.
Now, let's call the procedure sp_createUser:
If the procedure is executed successfully, then we'll commit the changes and return the success message.
Save the changes and restart the server. Go to the signup page and enter the name, email address and password and click the Sign Up button. On successful user creation, you'll be able to see a message in your browser console.
Wrapping It Up
Flask Web Development Miguel Grinberg
In this tutorial, we saw how to get started with creating a web application using Python Flask, MySQL and the Flask-MySQL extension. We created and designed the database tables and stored procedure, and implemented the signup functionality. In the next tutorial, we'll take this series to the next level by implementing sign-in functionality and some other features.
Source code from this tutorial is available on GitHub.
Do let us know your thoughts in the comments below!
Learn Python
Flask Documentation Pdf
Learn Python with our complete python tutorial guide, whether you're just getting started or you're a seasoned coder looking to learn new skills.