Site icon FahmidasClassroom

Web application using Python on Ubuntu

Web

Python has many packages to develop the web application. ‘web’ package is one of then. How you can install a web package of python and use it for developing web application are shown in this tutorial.

Install Web Framework:

At first check the current version of python.

$ python –version

if you have installed multiple versions of python then remove all versions and reinstall the python3+ again. You have to install pip3 before installing the python web framework. Run the following command to install pip3 for python3+.

$ sudo apt install python3-pip

Next, run the following command to install the web.py framework for python3.

$  sudo  pip3 install web.py==0.60

Set up web directories:

Run the following commands to create the necessary directories for the web application.

$ mkdir projects
$ cd projects
$ mkdir bin
$ cd bin
$ mkdir templates

Example-1: Create a simple web application

Create a file named app.py under /bin folder with the following script. It will run the local server at the port 8080 and display the value of greeting to the browser.

import web
urls = (
'/', 'index'
)

app = web.application(urls, globals())
class index:
	def GET(self):
		greeting = "Hello World"
		return greeting
if __name__ == "__main__":
	app.run()

Example-2: Create a simple web application with HTML file

Create an index.html file under /bin/templates folder with the following content to display content with the format.

$def with (greeting)
<html>
<head>
<title>Gothons Of Planet Percal #25</title>
</head>
<body>
$if greeting:
	I just wanted to say <em style="color: blue; font- size: 2em;">$greeting</em>.
$else:
	<em>Hello</em> World!
</body>
</html>

Create a file named app2.py under /bin folder with the following script. It will run the local server at the port 8080 and display the value of greeting by applying CSS to the browser.

import web
urls = (
'/', 'Index'
)
app = web.application(urls, globals())
render = web.template.render('templates/')
class Index(object):
	def GET(self):
		greeting = "Hello World"
		return render.index(greeting = greeting)
if __name__ == "__main__":
	app.run()

Example-3: Create a simple web application with a URL value

Create a file named app3.py under /bin folder with the following script. It will run the local server at the port 8080 and display the value of greeting and name send from the URL to the browser.

import web
urls = (
'/hello', 'Index'
)

app = web.application(urls, globals())
render = web.template.render('templates/')
class Index(object):
	def GET(self):
		form = web.input(name="Nobody")
		greeting = "Hello, %s" % form.name
		return render.index(greeting = greeting)
if __name__ == "__main__":
	app.run()

Run the following URL from any browser. It will not get any value of the name.

http://localhost:8080/hello

Again run the following URL from any browser. It will get the value of the name from the URL.

http://localhost:8080/hello?name=Frank

Example-4: Create a simple web application with multiple URL values

Create a file named app4.py under /bin folder with the following script. It will run the local server at the port 8080 and display the value of greeting and name send from the URL to the browser with an error.

import web
urls = (
'/hello', 'Index'
)

app = web.application(urls, globals())
render = web.template.render('templates/')
class Index(object):
	def GET(self):
		form = web.input(name="Nobody")
		greeting = "%s, %s" % (form.greet, form.name)
		return render.index(greeting = greeting)
if __name__ == "__main__":
	app.run()

Run the following URL from any browser.

http://localhost:8080/hello?name=Frank&greet=Hola

If any parameter is missing then an error list will be displayed that is solved in the next example,

Example-5: Create a simple web application with multiple URL values and error message

Create a file named app5.py under /bin folder with the following script. It will run the local server at the port 8080 and display the value of greeting and name send from the URL to the browser. If any value is missing then it will display an error.

import web
urls = (
'/hello', 'Index'
)

app = web.application(urls, globals())
render = web.template.render('templates/')
class Index(object):
	def GET(self):
		form = web.input(name="Nobody", greet=None)
		if form.greet:
			greeting = "%s, %s" % (form.greet, form.name)
			return render.index(greeting = greeting)
		else:
			return "ERROR: greet is required."
if __name__ == "__main__":
	app.run()

Run the following URL from any browser.

http://localhost:8080/hello?name=Frank

Example-6: Create a simple web application with HTML form

Create hello_form.html file under /bin/templates folder with the following content to display HTML form.

<html>
<head>
<title>Sample Web Form</title>
</head>
<body>
<h1>Fill Out This Form</h1>
<form action="/hello" method="POST">
A Greeting: <input type="text" name="greet">
<br/>
Your Name: <input type="text" name="name">
<br/>
<input type="submit">
</form>
</body>
</html>

Create a file named app6.py under /bin folder with the following script. It will run the local server at the port 8080 and display the value of greeting and name that will be submitted from the HTML form to the browser.

import web
urls = (
'/hello', 'Index'
)
app = web.application(urls, globals())
render = web.template.render('templates/')
class Index(object):
	def GET(self):
		return render.hello_form()
	def POST(self):
		form = web.input(name="Nobody", greet="Hello")
		greeting = "%s, %s" % (form.greet, form.name)
		return render.index(greeting = greeting)
if __name__ == "__main__":
	app.run()

Run the following URL from any browser. It will display the form.

http://localhost:8080/hello

Example-7: Create a simple web application with Layout

Modify index.html with the following code that will print formatted greeting.

$def with (greeting)
$if greeting:
	I just wanted to say <em style="color: green; font- size: 2em;">$greeting</em>.
$else:
	<em>Hello</em> World!

Modify hello_form.html with the following code that will take two inputs from the user.

<h1>Fill Out This Form</h1>
<form action="/hello" method="POST">
A Greeting: <input type="text" name="greet">
<br/>
Your Name: <input type="text" name="name">
<br/>
<input type="submit">
</form>

Create layout.html with the following code that contains the base layout.

$def with (content)
<html>
<head>
<title>Gothons From Planet Percal #25</title>
</head>
<body>
$:content
</body>
</html>

Create a file named app7.py under /bin folder with the following script. It will run the local server at the port 8080 and display the value of greeting and name that will be submitted from the HTML form to the browser.

import web
urls = (
'/hello', 'Index'
)

app = web.application(urls, globals())
render = web.template.render('templates/', base="layout")
class Index(object):
	def GET(self):
		return render.hello_form()
	def POST(self):
		form = web.input(name="Nobody", greet="Hello")
		greeting = "%s, %s" % (form.greet, form.name)
		return render.index(greeting = greeting)
if __name__ == "__main__":
	app.run()

Conclusion:

The uses of a Python Web Framework are shown in this tutorial to help the user to start the web application task using python.

The steps of this tutorial are shown in the following video link.

 

Exit mobile version