Skip to content

Commit 0898e72

Browse files
committed
Adds: python Web App
1 parent 9af10ad commit 0898e72

File tree

8 files changed

+141
-1
lines changed

8 files changed

+141
-1
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ This repository contains **Some mini projects** implemented in **Python**
99
* TIC TAC TOE game
1010
* Python Calculator
1111

12-
![image](https://raw.githubusercontent.com/its-Kumar/Python_Projects/master/Python_GUI/PYTHON%20CALCULATOR/calc.png)
12+
![image](Python_GUI/PYTHON%20CALCULATOR/calc.png)
1313

1414
* Youtube Video Downloader
1515

1616
![youtube](YoutubeDownloader/Screenshot.png)
1717

18+
### Python WebApp
19+
20+
[Pure Web Application](python_webapp/requirements.txt)
21+
1822
---
1923

2024
## Author

python_webapp/README.md

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Python_webapp
2+
3+
A Pure Python Web Application Without using any FrameWork.
4+
5+
6+
## Installation and Setup
7+
8+
1. install python 3.x
9+
2. install requirements
10+
`python -m pip install -r requirements.txt`
11+
OR
12+
`pip install -r requirements.txt`
13+
3. if this did not worked than try `python -m pip install gunicorn`
14+
4. run gunicorn server:
15+
`gunicorn server:app --reload`
16+
5. open the browser and go to the url which will apper
17+
```bash
18+
$ gunicorn server:app --reload
19+
[2020-11-19 13:14:49 +0530] [14209] [INFO] Starting gunicorn 20.0.4
20+
[2020-11-19 13:14:49 +0530] [14209] [INFO] Listening at: http://127.0.0.1:8000 (14209)
21+
[2020-11-19 13:14:49 +0530] [14209] [INFO] Using worker: sync
22+
[2020-11-19 13:14:49 +0530] [14212] [INFO] Booting worker with pid: 14212
23+
```
24+
25+
## Templates
26+
27+
Put your templates/ html files in `templates` directory.
28+
29+
## Views
30+
31+
Put your views/ functions in `views.py` file
32+
33+
34+
### App
35+
36+
Create a gunicorn web app using following function->
37+
38+
```python
39+
def app(environ, start_response):
40+
#data = "Hello World!"
41+
data = views.home(environ)
42+
data = data.encode("utf-8")
43+
start_response(
44+
f"200 OK", [
45+
#("Content-Type", "text/plain"),
46+
("Content-Type", "text/html"),
47+
("Content-Length", str(len(data)))
48+
]
49+
)
50+
return iter([data])
51+
```
52+
53+
### Developer
54+
**[Kumar Shanu](https://its-kumar.github.io/)**

python_webapp/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
gunicorn==20.0.4
2+

python_webapp/server.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# gunicorn server
2+
import views
3+
4+
5+
def app(environ, start_response):
6+
path = environ.get('PATH_INFO')
7+
if path.endswith("/"):
8+
path = path[:-1]
9+
if path == "": # index / root of the web
10+
data = views.home(environ)
11+
elif path == '/contact':
12+
data = views.contact_us(environ)
13+
else:
14+
data = views.render_template("error.html", context={"path": path})
15+
16+
#data = "Hello World!"
17+
data = data.encode("utf-8")
18+
start_response(
19+
f"200 OK", [
20+
#("Content-Type", "text/plain"),
21+
("Content-Type", "text/html"),
22+
("Content-Length", str(len(data)))
23+
]
24+
)
25+
return iter([data])

python_webapp/templates/contact.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Pure python webapp</title>
7+
</head>
8+
<body>
9+
<h1>Contact Us</h1>
10+
</body>
11+
</html>

python_webapp/templates/error.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Pure python web app</title>
8+
</head>
9+
10+
<body>
11+
<h1>Error 404: page not found! {path}</h1>
12+
</body>
13+
14+
</html>

python_webapp/templates/index.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Pure python web app</title>
7+
</head>
8+
<body>
9+
<h1>Hello {name}</h1>
10+
<h3>This is a pure Web Application in python without using any Framework</h3>
11+
<h4>Not Django, Not Flask, Not anyone</h4>
12+
<p>Developer: <a href="https://its-kumar.github.io/">Kumar Shanu</a></p>
13+
</body>
14+
</html>

python_webapp/views.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def render_template(template_name="index.html", context={}):
2+
html_str = ""
3+
with open('templates/'+template_name, "r") as f:
4+
html_str = f.read()
5+
html_str = html_str.format(**context)
6+
return html_str
7+
8+
9+
def home(environ):
10+
context = {"name": 'visiter'}
11+
return render_template(template_name="index.html", context=context)
12+
13+
14+
def contact_us(environ):
15+
context = {}
16+
return render_template(template_name="contact.html", context=context)

0 commit comments

Comments
 (0)