This repository has been archived by the owner on Oct 12, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
SingleClickAndRun
114 lines (88 loc) · 3.41 KB
/
SingleClickAndRun
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
Here is a simple application which runs as a standalone system:
{{{
#!python
import webbrowser
import cherrypy
class MyApp:
""" Sample request handler class. """
def index(self):
return """<html><head><title>An example application</title></head>
<body>
<h1>This is my sample application</h1>
Put the content here...
<hr>
<a href="/exit">Quit</a>
</body></html>"""
index.exposed = True
def exit(self):
raise SystemExit(0)
exit.exposed = True
cherrypy.tree.mount(MyApp())
cherrypy.engine.start(blocking=False)
cherrypy.server.socket_port = 8765
cherrypy.server.quickstart()
webbrowser.open("http://127.0.0.1:8765")
cherrypy.engine.block()
}}}
OK, so how does it work?
First of all, you have a standard !CherryPy application (class !MyApp). Then you have the startup code. After you start the server, use the webbrowser module to start a browser pointing at the application's home page. Then, block. The application will now run as a normal !CherryPy application.
Note the "Quit" link on the application page. This redirects the browser to the "/exit" URL, which stops the !CherryPy server.
Many of the ideas here came from "[http://mindview.net/WebLog/log-0045 Browser as Desktop UI]", which describes
a similar application, but using Python's SimpleHTTPServer module.
== py2exe ==
Here's a bare-bones example of how to bundle the above !CherryPy application using [http://starship.python.net/crew/theller/py2exe/ py2exe]. Assume that the above module is called app.py,
then you need a setup.py script something like this:
{{{
#!python
from distutils.core import setup
import py2exe
setup(
# The first three parameters are not required, if at least a
# 'version' is given, then a versioninfo resource is built from
# them and added to the executables.
version = "0.1.0",
description = "Sample CherryPy standalone app with py2exe",
name = "CherryPy sample",
# Exclude OpenSSL, unless we are building a https server
options = { 'py2exe': { 'excludes': 'OpenSSL', "packages": ["encodings", "email"] }},
# targets to build. You may want to make this "windows" rather than
# "console", to avoid a console window opening. But if you do, you
# need some way of showing log information
console = ["app.py"],
)
}}}
{{{
#!html
<h2 class='compatibility'>Older versions</h2>
}}}
== 2.2 ==
Replace:
{{{
cherrypy.tree.mount(MyApp())
cherrypy.engine.start(blocking=False)
cherrypy.server.socket_port = 8765
cherrypy.server.quickstart()
webbrowser.open("http://127.0.0.1:8765")
cherrypy.engine.block()
}}}
...with:
{{{
cpg.root = MyApp()
webbrowser.open("http://127.0.0.1:8765")
cpg.server.start(configMap = {'socketPort': 8765})
}}}
Assuming no timing problems (it works on my machine) the browser will start up showing the application main page. If there are timing problems, you can just wrap the webbrowser call in a timer, to delay it by a second or so. Example: {{{threading.Timer(1, webbrowser.open, ("http://127.0.0.1:8765",)).start()}}}
== 2.1 ==
Replace {{{raise SystemExit(0)}}} with:
{{{
import threading
threading.Timer(1, cherrypy.server.stop).start()
return """<html><head><title>System Terminated</title></head>
<body>
<h1 align="center">System Terminated</h1>
</body>
</html>
"""
}}}
== 2.0 ==
Replace {{{import cherrypy}}} with {{{from cherrypy import cpg as cherrypy}}}