Skip to content

Instantly share code, notes, and snippets.

@ingoogni
Last active July 29, 2020 06:28
Show Gist options
  • Save ingoogni/abbe80044b655010139a40d7afbd3413 to your computer and use it in GitHub Desktop.
Save ingoogni/abbe80044b655010139a40d7afbd3413 to your computer and use it in GitHub Desktop.
CherryPy config & structure
```
from .Thing import Thing
```
Goal: create a cherrypy app in such a way it can be "dropped" on the server as is, only change an __init__.py for importing. Simmilar with tools & plug ins.
Server
|
+-- app
| |
| +-- __init.py__
| |
| +-- Thing
| | |
| | +--- static
| | | |
| | | +-- screen.css
| | | +-- thing.js
| | |
| | +-- __init__.py (empty)
| | +-- Thing.py
| | +-- index.html
| |
| +-- OtherApp
|
|
+--- server.py
+--- server.conf
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">
<link rel="stylesheet" type="text/css" href="/thing/static/screen.css" media="screen">
<script src="/thing/static/order.js" type="text/javascript"></script>
<title>CherryPy</title>
</head>
<body>
<p>...World...</p>
</body>
</html>
```
import os
import cherrypy
server_root = os.path.dirname(os.path.abspath(__file__))
global_config = os.path.join(server_root, 'server.conf')
cherrypy.config.update(global_config)
import app
cherrypy.engine.signals.subscribe()
cherrypy.engine.start()
cherrypy.engine.block()
```
```
import os
import cherrypy
app_dir = os.path.dirname(__file__)
@cherrypy.expose
class Thing(object):
def __init__(self):
self.app_name = 'thing' #
self.app_dir = app_dir # instead of here these could be stuck in global or app config
@cherrypy.tools.accept(media='text/plain')
def GET(self, *args, **kwargs):
os.chdir(self.app_dir)
return open('index.html')
thing_conf = {
'/': {
'request.dispatch' : cherrypy.dispatch.MethodDispatcher(),
'tools.staticdir.root' : app_dir
},
'/static': {
'tools.staticdir.dir' : "static",
'tools.staticdir.on' : True,
'tools.staticdir.debug' : True
}
}
cherrypy.tree.mount(Thing(), '/thing', thing_conf)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment