![]() ![]() Line 9: When you run your Python script, Python assigns the name “_main_” to the script when executed. Line 6–7: When the user goes to my website and they go to the default page (nothing after the slash), then the function below will get activated. Then this will be the default page of Google. For example, if I go to a website such as “/” with nothing after the slash. Here we are creating a new web application. We are creating an instance of the Flask class and calling it app. This current file will represent my web application. Line 1: Here we are importing the Flask module and creating a Flask web server from the Flask module. This piece of code is stored in our main.py. The code lets us run a basic web application that we can serve, as if it were a website. Flask lets us focus on what the users are requesting and what sort of response to give back. It makes the process of designing a web application simpler. What is Flask? Flask (A Python Microframework) It will also figure out what response to send to the user. It will figure out what those requests are dealing with and what they are asking. We will write code that will take care of the server side processing. Then it displays what you asked for on a page in the browser. The Google Server sends back an HTTP response that contains the information that my web browser receives. The Google Server receives the request and needs to figure how to interpret that request. What happens is that an HTTP request has been sent to a server.įor example, when I go to my address bar and type, then hit enter, an HTTP request is sent to a Google Server. When you type the name of a website in the address bar of your browser and you hit enter. Let me give you an example of how you use it everyday. The internet uses it to interact and communicate with computers and servers. What is HTTP and What Does it Have to do with Flask? All the code used in the tutorial is available in this GitHub Repository. Return render_template( "list.In each section, I will show pieces of code for you to follow along. Return render_template( "result.html",msg = list (): 1įrom flask import Flask, render_template, request ![]() The following is the complete code for the Flask-SQLite application. Return render_template( "list.html",rows = rows)įinally, the ‘/‘ URL rule renders ‘home.html’, which is the entry point of the application. The application contains another list () function represented by the ‘/list’ URL.It populates’rows’ as a Multidict object that contains all records in the student table.This object is passed to the list.html template. ![]() The HTML script for result.html contains an escape statement, which displays the result of the Insert operation. Return render_template( "result.html",msg = msg) = )Ĭur.execute( "INSERT INTO students (name,addr,city,pin) The addrec () function retrieves the form’s data through the POST method and inserts the student table.The message corresponding to the success or error in the insert operation will be rendered as ‘result.html’. new_student ():Īs can be seen, the form data is published to the ‘/addrec’ URL of the binding addrec () function. The first new_student() function is bound to a URL rule ( '/enternew').It presents an HTML file that contains a student information form. Our Flask application has three View functions. 1Ĭonn.execute( 'CREATE TABLE students (name TEXT, addr TEXT, city TEXT, pin TEXT)') The program creates a SQLite database ‘database.db ‘ where the student tables are created. You can create an SQLite database from Python code. The SQLite database storse all data in a single file. Related course: Python Flask: Create Web Apps with Flask Create database and table Each database can have tables and each table can have records. SQLite is a relational database system that uses the SQL query language to interact with the database. W the Flask application interacts with SQLite. The SQlite3 module comes with the Python release. ![]()
0 Comments
Leave a Reply. |