a few examples

This commit is contained in:
2019-06-18 08:51:21 +02:00
parent 3531d493d8
commit 85f1a27e94
4 changed files with 49 additions and 4 deletions

View File

@@ -4,7 +4,10 @@ import cherrypy
class HelloWorld(object): class HelloWorld(object):
@cherrypy.expose @cherrypy.expose
def index(self): def index(self):
return "Hello World!" with open('./templates/index.html') as fh:
index_f = fh.read()
result = index_f.format(_title='Nazev_stranky', _nadpis='Cokoliv!')
return result
cherrypy.quickstart(HelloWorld()) cherrypy.quickstart(HelloWorld())

View File

@@ -28,7 +28,7 @@ def main_server_loop():
*Exception* If server is unable to start *Exception* If server is unable to start
''' '''
server_config={ server_config = {
'server.socket_host': '127.0.0.1', 'server.socket_host': '127.0.0.1',
'server.socket_port': 80 'server.socket_port': 80
} }

View File

@@ -1,6 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
import cherrypy import cherrypy
import config import json
static_dir = '/templates/' # Needs to have trailing and leading slash '/' static_dir = '/templates/' # Needs to have trailing and leading slash '/'
@@ -8,5 +8,37 @@ class wellcome(object):
'''Base Index constructor and expose function''' '''Base Index constructor and expose function'''
@cherrypy.expose @cherrypy.expose
def index(self): def index(self):
result = '<h1>Wellcome</h1>' result = '''{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 27,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
},
{
"type": "mobile",
"number": "123 456-7890"
}
],
"children": [],
"spouse": null
}'''
return json.loads(result)
@cherrypy.expose
def other(self):
result = '<h1>Other</h1>'
return result return result

View File

@@ -0,0 +1,10 @@
<hrml>
<head>
<title>{_title}</title>
</head>
<body>
<div>
<h1>{_nadpis}!</h1>
</div>
</body>
</hrml>