-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello.py
More file actions
132 lines (107 loc) · 4.56 KB
/
hello.py
File metadata and controls
132 lines (107 loc) · 4.56 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
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
# 'hello_world' is the function that's called when we open up
# http://localhost:8080/hello/bar in the browser.
# We call the 'hello_world' function a 'view'. Views accept 'Request'
# objects and return 'Response' objects.
def hello_world(request):
# The request.matchdict is a dictionary that has a 'name' entry,
# which corresponds to the '{name}' part in our '/hello/{name}'
# route below.
some_string = """
<html>{}<body>{}{}</body></html>
""".format(
Head('Python Rocks!', 'header.css', 'footer.css', 'main.js'),
Div(divclass='myclass', divbody='<h1>Hello Py</h1>'),
myfunc(generate_block(6, 'main_box_style'))
)
return Response(some_string)
def myfunc(func):
"""function to create string
from result of generate_block() function
which is a generator object with Div class
instances inside"""
result = ''
for i in func:
result += i
return result
def generate_block(count, divclass):
"""function that generates instances
of Div class, count must be int of how many
instances you need
return generator object
"""
text = 'Lorem Ipsum Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse tincidunt ultrices leo, eu aliquam mauris placerat ac. Praesent quis lacus quis arcu aliquet ornare euismod ac nibh.'
for i in range(count):
divid = '{}-{}'.format(divclass, i)
yield Div(divclass=divclass, divid=divid, divbody=text).__str__()
class Div(object):
"""class that creates html divs
divbody is optional
"""
def __init__(self, divclass='empty', divid='', divbody=''):
if len(divid) >= 1:
self.div_open = '<div class={} id={}>'.format(divclass, divid)
else:
self.div_open = '<div class={}>'.format(divclass)
self.div_body = '{}'.format(divbody)
self.div_close = '</div>'
def return_div(self):
return '{open}{body}{close}'.format(open=self.div_open, body=self.div_body, close=self.div_close)
def __str__(self):
return self.return_div()
class Head(object):
"""class that creates html header
with:
title it must be string
and a list of string arguments
that will be used as css stylesheet
or link to js file based on file extension"""
def __init__(self, title, *args):
self.head_open = '<head>'
self.head_close = '</head>'
self.title = '<title>{}</title>'.format(title)
self.css = ''
self.js = ''
for each in args:
if each[-3:] == 'css':
self.css += '<link rel="stylesheet" href="{}" type="text/css">'.format(each)
elif each[-2:] == 'js':
self.js += '<script src="{}" type="text/javascript"></script>'.format(each)
else:
pass
def get_css(self):
return '{}{}{}{}{}'.format(self.head_open, self.css, self.js, self.title, self.head_close)
def __str__(self):
return self.get_css()
# Views may also return dictionaries:
def hello_json(request):
return request.matchdict
if __name__ == '__main__':
# Instantiate a Pyramid 'Configurator' object. This one is used
# to configure our views, and we'll use it ultimately to create
# our WSGI application:
config = Configurator()
# This is the route configuration for our 'hello_world' view.
# Note that the pattern has a variable part '{name}'. Once our
# view function is called, we can access the 'name' as
# 'request.matchdict["name"]'.
config.add_route(name='hello', pattern='/hello/{namesss}')
# Next, we need to register our function as a view using the
# 'hello' route we just defined:
config.add_view(hello_world, route_name='hello')
# Let's register another, slightly different route and associate
# it with our second view: 'hello_json'.
config.add_route(name='hello_json', pattern='/json/{name}')
# Note that this time, we pass an additional 'renderer' argument
# to 'config.add_view'. This tells Pyramid to take the dictionary
# that we return in our 'hello_json' function and turn it into a
# JSON response.
config.add_view(hello_json, route_name='hello_json', renderer='json')
# We can now create our WSGI app and serve it using the standard
# library 'wsgiref' server:
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 8080, app)
print("Server started at http://localhost:8080")
server.serve_forever()