Python+flask development Small White the next day

Source: Internet
Author: User

Compiling a Python web page using Vscode

1. Start with the basics, about the use of Vscode:
Run the Python program and run the Java,c,c++ program, you need to create a new file, the first file is not recommended to create a new local C drive, inconvenient to find, file path will be accidentally cumbersome, for the development of the later inconvenience!
The first code is from the https://dormousehole.readthedocs.io/en/latest/in the quick get to the article, so the name of the building and the inside of the same, easy to understand, the folder in the D-Disk named app.py, ". py "is a unified extension of the Python file, the previous app location suggested to use a meaningful English name, do not use flask, which will conflict with the flask itself
Open Vscode, click File, open app.py file
Running the following code is also the simplest HelloWorld:

from flask import Flaskapp = Flask(__name__)@app.route(‘/‘)def hello_world(): return ‘Hello World!‘if __name__ == ‘__main__‘: app.run()

Be careful with writing, because Python is strictly indented, and underscores, name and main are double-underlined,
"From flask import flask" First we imported the Flask class. An example of this class will be our WSGI application. (Wsgi:web server Gateway Interface (Python Web server, Gateway Interface, abbreviated as WSGI) is an interface between a Python application or a framework and a WEB server)
We then created an instance of this class. The first parameter is the name of the application module or package. If you are using a single module (as in this example), you should use _name_, because the name changes depending on whether the module is used as applied or as a module, possibly ' _Main_ ', may also be the name of the actual import). This parameter is required so that Flask can know where to find templates and static files.
We then use the root () adorner to tell flask the URL of the departure function;
"Def" is used to define the name of the subsequent Hello_world function function to generate the associated URL and return the information that needs to be displayed in the user's browser (return ' Hello world! ’)
Finally run the server and our local app using Run ()
"If _name= 'Main_ ' "This is to ensure that the server will only run under the Python interpreter, not as a module when imported;
At last run, open the. bat in Cygwin and enter the following command after you double-click Run:
SOURCE Pyvenv/bin/activate (Press Enter): Venv is a standalone Python runtime that we created can enter this environment with source
cd/cygdrive/d/app.py (after Cygdrive is the location of the new package/x/xxx.xx continue to press ENTER after the loss): CD is the abbreviation for change directory (changing directory). Its function is to change the current working directory, cygdrive/d means that the entire D-disk under your Windows is Cygwin mounted to/cygdrive/d meaning
Python3 app.py (Press the ENTER key)
Run the correct regrets appear a line in English there is a link click Copy and paste in our browser's URL bar will appear we want Hello world this English sentence.
2. A few useful links:
1. PEP8 Python Coding Specification: https://www.douban.com/note/134971609/
2. Flask learn to use the template: http://blog.csdn.net/bestallen/article/details/52055061
Also open Vscode after clicking on the file-preferences-keyboard shortcuts will have a very long and full of common compiler shortcut keys, such as Ctrl+s is saved; Ctrl + Z is back to the previous step;
3. Use Python to make a simple Web page:
You need a bootstrap package that contains three folders:

and a jquery. js file
In the app.py folder we built in the D-disk, a folder called Static, built into a folder called Bootstrap to put the prepared bootstrap in the three folders and jquery files in,

Like this, we can see these papers in the Vscode.
We then run the following code in app.py:

from Flask import Flask,render_templateapp = Flask (__name__) @ App.route ('/') def index (): return render_template ( ' login.html ' , Name= @app. Route ('/ABC ') def login  (): return  ' Hey ' if __name__ = =  ' __main__ ': App.run ()      

Render_template is a template equivalent to our mathematical formula this is a template that already exists in the Python library and we can invoke the function of adding more of what we want to output in our Python without needing one to return; In fact, the function of Render_template is to introduce the index.html, and then modify and render the HTML according to the parameters passed in later.
There is a detailed explanation about @app.route and other adorners in this article; http://python.jobbole.com/80956/
We need our python to run out of the page more beautiful! This is where we use template templates in Python, which is a template that can be used to make Web pages;
We first create a new file called templates under our app.py folder, create a new HTML file called login under this file, and start writing our web page.

<! DOCTYPE html><Html><Head><Metacharset="Utf-8"/><Metahttp-equiv="X-ua-compatible"Content="Ie=edge" ><Title>page Title</Title><MetaName="Viewport"Content="Width=device-width, initial-scale=1" ><Title>bootstrap 101 Template</Title><!--Bootstrap--<Linkhref="/static/bootstrap/css/bootstrap.min.css"Rel="Stylesheet" ><!--HTML5 Shim and Respond.js for IE8 support of HTML5 elements and media queries-<!--WARNING:Respond.js doesn ' t work if you view the page via file://--<!--[if Lt IE 9]> <script src= "Https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js" ></script > <script src= "https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js" ></script><! [endif]--></Head><Body><!--jQuery (necessary for Bootstrap ' s JavaScript plugins)--<ScriptSrc="/static/bootstrap/jquery-1.12.4.js" ></Script><!--Include all compiled plugins (below), or include individual files as needed-<ScriptSrc="/static/bootstrap/js/bootstrap.min.js" ></Script><BodyBgcolor="#EEE5DE ><div class="Container "><Divclass="Now" ><Divclass="Cd-md-4" ><Divclass="Panel Panel-danger" ><Divclass="Panel-body" ><Marquee><B><FontColor="#EE6AA7" ><h3> Hello, please login!</H3></Font></B></Marquee><!--merry Effects--</Div></Div></Div></Div></div><form class= "Text-center" > Name:<< Span class= "Hljs-title" >input type= "text" name=" name "> <br /> Password:<input tupe= "password" name= "password" ></form></ Body></HTML>       

Click Ctrl+s Save after using the Cygwin. bat to run the Web address after the URL is pasted into the URL bar on the above code is a very simple text containing a string of merry effects and form a login page
Throw two interesting useful links:
Https://www.cnblogs.com/zendu/p/4991090.html
Http://www.114la.com/other/rgb.htm
Today I learned so much not to write a backache

Python+flask development Small White the next day

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.