Python

champak
₹2,000.00 ₹300.00
The Python Programming Language at Champak's World
  • 24 students
  • 41 lessons
  • 0 quizzes
  • 10 week duration

Python

Using Flask-1

Flask is a web application framework written in Python. Armin Ronacher, who leads an international group of Python enthusiasts named Pocco, develops it. Flask is based on Werkzeug WSGI toolkit and Jinja2 template engine. Both are Pocco projects.

What is WSGI?
Web Server Gateway Interface (WSGI) has been adopted as a standard for Python web application development. WSGI is a specification for a universal interface between the web server and the web applications.

What is Werkzeug?

Werkzeug
It is a WSGI toolkit, which implements requests, response objects, and other utility functions. This enables building a web framework on top of it. The Flask framework uses Werkzeug as one of its bases.

What is Jinga2?

Jinga2
Jinga2 is a popular templating engine for Python. A web templating system combines a template with a certain data source to render dynamic web pages.

Flask is often referred to as a micro framework. It aims to keep the core of an application simple yet extensible. Flask does not have built-in abstraction layer for database handling, nor does it have form a validation support. Instead, Flask supports the extensions to add such functionality to the application


from flask import Flask
app = Flask(__name__)

@app.route("/")

def index():




return "<h1>Hello from Flask</h1>"

if __name__ == "__main__":
#print(__name__)
app.run(host="localhost", port=int("777"))
#app.run()

This will create a simple server in Python and its output will be available at

http://localhost:777/

 

 

To add another URL. Create another route, create a function and return an output.


@app.route("/champak/")
def champak():
return "<h1>Hello from Champak</h1>"

The full code now.


from flask import Flask
app = Flask(__name__)

@app.route("/")

def index():




return "<h1>Hello from Flask</h1>"

@app.route("/champak/")
def champak():
return "<h1>Hello from Champak</h1>"

if __name__ == "__main__":
#print(__name__)
app.run(host="localhost", port=int("777"))
#app.run()

 

Next step. We will create a html file and send its output to the browser.

To begin with create a directory called templates in the location where your python file is stored. Create a html file and write some html code.


<!DOCTYPE html>
<html>
<head>
<title>Champak's World</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>
<h1>Welcome to Champak's World</h1>
<h2>The best resource for Python round the world</h2>
</div>
</body>
</html>

Change the code for the champak route

@app.route("/champak/")
def champak():
    return render_template("champak.html")

 

Let us add a picture.
To do this create a directory called static in the same location as the templates directory and add a picture file there.

Put a picture in the static directory.

 

Now, change the code of the html file.


<!DOCTYPE html>
<html>
<head>
<title>Champak's World</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>
<h1>Welcome to Champak's World</h1>
<h2>The best resource for Python round the world</h2>
<h2>This is Champak</h2>
<img src="/static/champak.jpg"/>
</div>
</body>
</html>

 

 

External CSS and scripts will be added in the same way.


.imgclass
{
border-radius: 50%;
}

Add it to the html file


<!DOCTYPE html>
<html>
<head>
<title>Champak's World</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/static/mystyle.css" rel="stylesheet"/>
</head>
<body>
<div>
<h1>Welcome to Champak's World</h1>
<h2>The best resource for Python round the world</h2>
<h2>This is Champak</h2>
<img src="/static/champak.jpg" class="imgclass"/>
</div>
</body>
</html>

Accessing form data. We have added a form to the html page.


<!DOCTYPE html>
<html>
<head>
<title>Champak's World</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/static/mystyle.css" rel="stylesheet"/>
</head>
<body>
<div>
<h1>Welcome to Champak's World</h1>
<h2>The best resource for Python round the world</h2>
<h2>This is Champak</h2>
<img src="/static/champak.jpg" class="imgclass"/>
<form action="/form/" method="post">
<input type="text" name="n"/>

<input type="submit" value="Enter"/>
</form>
</div>
</body>
</html>

 

The updated Python code.


from flask import Flask,render_template,request
app = Flask(__name__)

@app.route("/")

def index():




return "<h1>Hello from Flask</h1>"

@app.route("/champak/")
def champak():
return render_template("champak.html")
@app.route("/form/", methods=['POST'])
def form():
return "Champak" + str(request.form["n"])

if __name__ == "__main__":
#print(__name__)
app.run(host="localhost", port=int("777"))
#app.run()

end

Leave a Reply

Your email address will not be published. Required fields are marked *