(This page has no text content)
Flask Web Development from Scratch
Flask Web Development from Scratch 1. Introduction 2. Completed image of the blog application 3. Installing Python 4. Installing pip 5. Try running the application in a single file 6. Installing Pipenv 7. Create the startup file 8. Create a config file - allows you to work with settings together 9. Understanding the Flask Framework - MTV Framework 10. Create Templates - Templates in the MTV Framework 11. Introducing Bootstrap - Clean Your Design 12. Create a login form - implement a form 13. Create a View - View in the MTV framework 14. Create a base layout template 15. Work with session - Enables secure login with authentication 16. Add flash - Display a message to the user 17. url_for - Automatically Create Links 18. Work with databases 19. Model - Model in the MTV framework 20. Create scripts - make certain actions common 21. Learn about CRUD 22. Create a blog posting function - Create in CRUD 23. Create a blog list feature - Read in CRUD 24. Create a blog detail feature - Read in CRUD 25. Create a blog editing feature - Update in CRUD 26. Create a blog post delete feature - Delete in CRUD 27. Add static files - allows you to work with images and stylesheets 28. Create a decorator for login authentication 29. Split an Application with Blueprint 30. Write Unit Tests 31. Measuring and Reporting Test Coverage
32. Final application structure 33. Chapter of Closing
Introduction Flask is a lightweight web application framework with Python. The Flask feature allows applications to be created in a single file. This is called a microframework. On top of that, it has the scalability to build large applications. This book is about building applications from scratch. I wrote it so that you can learn all the necessary technologies. It is based on the experience of actually operating the application developed in Flask. I think there are a wide range of situations where the knowledge obtained here can be applied. Policy in This Book The following points have been made into a policy for the organization of this book. Read through and cover the basics of creating web applications with Flask Finally, you’ll have a working blog application
Organized into one chapter on one topic for future reference System Environment Here’s my system environment for reference. Python: 3.6.3 Sample Code Sample code is available at: https://github.com/chaingng/flask_tutorial_en Feedback We would appreciate it if you could send us any typos or feedback. We will update as much as possible. takatomo.honda.0103@gmail.com
Completed image of the blog application In this book, you create a blog application from scratch. The completed image is as follows: When you access the top page, the login screen appears. After you enter your login ID and password, the blog list screen appears.
Click New Post to open the Post screen and create a new article. Click an article from the blog list screen to display the article details. You can also edit and delete articles from this screen.
If you press the edit button, you can edit the article. Press the delete button to delete the article.
Installing Python Installing on MacOS On latest version of MacOS, Python is installed by default. If you type python --version and the version information appears, you don’t need to install it. If not, install it using the brew command. If you don’t see the version information after typing brew --version, install brew as follows: Installing brew Install the Xcode command-line tools. xcode-select --install Install brew. /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/insta ll)" Verify that brew is installed. brew --version Homebrew 1.5.14 Homebrew/homebrew-core (git revision 26ca1; last commit 2018- 04-02) Installing Python Finally, install python using brew. brew install python
On Linux On Linux, python is also a standard installation. If not, install it as follows:. On Ubuntu sudo apt install python On Redhat, CentOS sudo yum install python If python --version displays version information, the installation is successful.
Installing pip pip is a package management system for managing external packages in Python. With pip, you can easily install and use useful packages, including Flask itself, in your applications. In Python 3.4 and later, pip is installed by default, so no installation is necessary. If not, you can install it by running the following command: curl -kL https://bootstrap.pypa.io/get-pip.py | sudo python Type pip --version and the version number is displayed. $ pip --version pip 9.0.1 from /Users/hondatakatomo/.pyenv/versions/3.6.3/lib/python3.6/site- packages (python 3.6)
Try running the application in a single file Install Flask using the pip described in the previous chapter. pip install Flask Make a file named hello.py with the following contents: # hello.py from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return "Hello World!" if __name__ == '__main__': app.run() From the console, type: $ python hello.py * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) The application is now up and running. Go to http://127.0.0.1:5000/. If it looks like this, it is successful.
You have now created a Flask application with just one file. Next, you’ll create a blog application in Flask. Create a folder for the application. Name it application.
Installing Pipenv What is Pipenv? Pipenv is a library that allows you to automatically create your own environment (Virtual environments) for each project and manage packages for each virtual environment. In particular, we used the pip command to install and manage python packages on an entire PC. Pipenv allows you to create your own environment for each project (In other words, for each folder you create). Typically, if you have multiple projects on one machine, it is difficult to separate the environments for each project. On the other hand, Pipenv allows you to create your own environment without affecting other projects. Installing Pipenv To install Pipenv, run the following command: pip install pipenv Type pipenv --version and the version number will indicate a successful installation. $ pipenv --version pipenv, version 9.1.0 Now that you have Pipenv installed, I’ll use it all from now on without using pip. Initial application settings
Then go to the application folder you created earlier and execute the following command: pipenv --three Now, under the application folder, you have a dedicated virtual environment for projects running Python3. At the same time, a file named Pipfile/Pipfile.lock is created that contains the package information. Let’s actually go into a virtual environment. pipenv shell Now the project-specific virtual environment is running, and independent development can be done on this project alone without affecting the state of the machines running it. Finally, install the Flask package, which is not included in this virtual environment. pipenv install Flask
Create the startup file Now, let’s start creating a blog application. You just created an application with one file. On the other hand, when creating a large application, it is easier to update and a better application to create separate files for each function. First of all, let’s create the “hello, world” application just by separating the startup files. At the end of this chapter, you will create the files in the following folder structure. At the end of the previous chapter, Pipfile and Pipfile.lock already exist. application/ |- Pipfile |- Pipfile.lock |- server.py |- flask_blog/ |- __init__.py |- views.py Creating __init__.py Create a folder named flask_blog and a file named __init__.py in it. # flask_blog/__init__.py from flask import Flask app = Flask(__name__) import flask_blog.views In the first line, from flask import Flask, import the Flask itself. Then app = Flask(__name__) to create the Flask application body. Finally, import flask_blog.views to import a file called views that you will create.
Creating views.py Next, create a file named views.py under the flask_blog folder. # flask_blog/views.py from flask_blog import app @app.route('/') def show_entries(): return "Hello World!" This file describes what to do when a request is made to http://127.0.0.1:5000/. First, import the app created in __init__.py as from flask_blog import app. Next, in view, you can write something like this to describe what happens when a URL is accessed. @app.route(URL) def xxxx(): your process This time, writing @app.route('/') will invoke the method show _entries() when a request is made to http://127.0.0.1:5000/. By show_entries(), Hello World! is returned and displayed in the browser. Creating server.py Finally, create a startup file server.py under the application folder with the following contents: # server.py from flask_blog import app if __name__ == '__main__': app.run(debug=True)
Import app first, as you did when you created views.py. You can then write the following to describe what happens when this file is executed directly (python server.py): if __name__ == '__main__': your process This time, we want to start the application as python server.py as a startup file. It contains only one line: app.run(debug=True). Compared to the previous chapter, debug = True has been added. This means that you start the application in debug mode, which gives you more information on the console screen when you run the application. Finally, try starting the server with server.py. $ python server.py * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 167-157-088 As in the previous chapter, “Hello, world!” is displayed, it is successful.
(This page has no text content)
Comments 0
Loading comments...
Reply to Comment
Edit Comment