-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path26_python_web.py
More file actions
296 lines (244 loc) · 7.77 KB
/
26_python_web.py
File metadata and controls
296 lines (244 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# Setting up your project directory
#pip install virtualenv
# Creating routes
# Home routes
# let's import the flask
from flask import Flask
import os # importing operating system module
app = Flask(__name__)
@app.route('/') # this decorator create the home route
def home ():
return '<h1>Welcome</h1>'
@app.route('/about')
def about():
return '<h1>About us</h1>'
if __name__ == '__main__':
# for deployment we use the environ
# to make it work for both production and development
port = int(os.environ.get("PORT", 5000))
app.run(debug=True, host='0.0.0.0', port=port)
# Let's add additional route. Creating about route
# let's import the flask
from flask import Flask
import os # importing operating system module
app = Flask(__name__)
@app.route('/') # this decorator create the home route
def home ():
return '<h1>Welcome</h1>'
@app.route('/about')
def about():
return '<h1>About us</h1>'
if __name__ == '__main__':
# for deployment we use the environ
# to make it work for both production and development
port = int(os.environ.get("PORT", 5000))
app.run(debug=True, host='0.0.0.0', port=port)
# Creating templates
# home.html
# <!DOCTYPE html>
# <html lang="en">
# <head>
# <meta charset="UTF-8" />
# <meta name="viewport" content="width=device-width, initial-scale=1.0" />
# <title>Home</title>
# </head>
# <body>
# <h1>Welcome Home</h1>
# </body>
# </html>
# about.html
# <!DOCTYPE html>
# <html lang="en">
# <head>
# <meta charset="UTF-8" />
# <meta name="viewport" content="width=device-width, initial-scale=1.0" />
# <title>About</title>
# </head>
# <body>
# <h1>About Us</h1>
# </body>
# </html>
# Python Script
# app.py
# # let's import the flask
# from flask import Flask, render_template
# import os # importing operating system module
# app = Flask(__name__)
# @app.route('/') # this decorator create the home route
# def home ():
# return render_template('home.html')
# @app.route('/about')
# def about():
# return render_template('about.html')
# if __name__ == '__main__':
# # for deployment we use the environ
# # to make it work for both production and development
# port = int(os.environ.get("PORT", 5000))
# app.run(debug=True, host='0.0.0.0', port=port)
# Navigation
# <ul>
# <li><a href="/">Home</a></li>
# <li><a href="/about">About</a></li>
# </ul>
# # let's import the flask
# from flask import Flask, render_template, request, redirect, url_for
# import os # importing operating system module
# app = Flask(__name__)
# @app.route('/') # this decorator create the home route
# def home ():
# techs = ['HTML', 'CSS', 'Flask', 'Python']
# name = '30 Days Of Python Programming'
# return render_template('home.html', techs=techs, name = name, title = 'Home')
# @app.route('/about')
# def about():
# name = '30 Days Of Python Programming'
# return render_template('about.html', name = name, title = 'About Us')
# @app.route('/post')
# def post():
# name = 'Text Analyzer'
# return render_template('post.html', name = name, title = name)
# if __name__ == '__main__':
# # for deployment
# # to make it work for both production and development
# port = int(os.environ.get("PORT", 5000))
# app.run(debug=True, host='0.0.0.0', port=port)
# home.html
# <!DOCTYPE html>
# <html lang="en">
# <head>
# <meta charset="UTF-8" />
# <meta name="viewport" content="width=device-width, initial-scale=1.0" />
# <title>Home</title>
# </head>
# <body>
# <ul>
# <li><a href="/">Home</a></li>
# <li><a href="/about">About</a></li>
# </ul>
# <h1>Welcome to {{name}}</h1>
# {% for tech in techs %}
# <ul>
# <li>{{tech}}</li>
# </ul>
# {% endfor %}
# </body>
# </html>
# about.html
# <!DOCTYPE html>
# <html lang="en">
# <head>
# <meta charset="UTF-8" />
# <meta name="viewport" content="width=device-width, initial-scale=1.0" />
# <title>About Us</title>
# </head>
# <body>
# <ul>
# <li><a href="/">Home</a></li>
# <li><a href="/about">About</a></li>
# </ul>
# <h1>About Us</h1>
# <h2>{{name}}</h2>
# </body>
# </html>
# layout.html
# <!DOCTYPE html>
# <html lang="en">
# <head>
# <meta charset="UTF-8" />
# <meta name="viewport" content="width=device-width, initial-scale=1.0" />
# <link
# href="https://fonts.googleapis.com/css?family=Lato:300,400|Nunito:300,400|Raleway:300,400,500&display=swap"
# rel="stylesheet"
# />
# <link
# rel="stylesheet"
# href="{{ url_for('static', filename='css/main.css') }}"
# />
# {% if title %}
# <title>30 Days of Python - {{ title}}</title>
# {% else %}
# <title>30 Days of Python</title>
# {% endif %}
# </head>
# <body>
# <header>
# <div class="menu-container">
# <div>
# <a class="brand-name nav-link" href="/">30DaysOfPython</a>
# </div>
# <ul class="nav-lists">
# <li class="nav-list">
# <a class="nav-link active" href="{{ url_for('home') }}">Home</a>
# </li>
# <li class="nav-list">
# <a class="nav-link active" href="{{ url_for('about') }}">About</a>
# </li>
# <li class="nav-list">
# <a class="nav-link active" href="{{ url_for('post') }}"
# >Text Analyzer</a
# >
# </li>
# </ul>
# </div>
# </header>
# <main>
# {% block content %} {% endblock %}
# </main>
# </body>
# </html>
# about.html
# {% extends 'layout.html' %} {% block content %}
# <div class="container">
# <h1>About {{name}}</h1>
# <p>
# This is a 30 days of python programming challenge. If you have been coding
# this far, you are awesome. Congratulations for the job well done!
# </p>
# </div>
# {% endblock %}
# post.html
# {% extends 'layout.html' %} {% block content %}
# <div class="container">
# <h1>Text Analyzer</h1>
# <form action="https://thirtydaysofpython-v1.herokuapp.com/post" method="POST">
# <div>
# <textarea rows="25" name="content" autofocus></textarea>
# </div>
# <div>
# <input type="submit" class="btn" value="Process Text" />
# </div>
# </form>
# </div>
# {% endblock %}
# # let's import the flask
# from flask import Flask, render_template, request, redirect, url_for
# import os # importing operating system module
# app = Flask(__name__)
# # to stop caching static file
# app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
# @app.route('/') # this decorator create the home route
# def home ():
# techs = ['HTML', 'CSS', 'Flask', 'Python']
# name = '30 Days Of Python Programming'
# return render_template('home.html', techs=techs, name = name, title = 'Home')
# @app.route('/about')
# def about():
# name = '30 Days Of Python Programming'
# return render_template('about.html', name = name, title = 'About Us')
# @app.route('/result')
# def result():
# return render_template('result.html')
# @app.route('/post', methods= ['GET','POST'])
# def post():
# name = 'Text Analyzer'
# if request.method == 'GET':
# return render_template('post.html', name = name, title = name)
# if request.method =='POST':
# content = request.form['content']
# print(content)
# return redirect(url_for('result'))
# if __name__ == '__main__':
# # for deployment
# # to make it work for both production and development
# port = int(os.environ.get("PORT", 5000))
# app.run(debug=True, host='0.0.0.0', port=port)