Module jyserver.Bottle
24 | Module for using jyserver in Bottle. This module provides two new 27 | decorators.
28 |Decorators
29 |-
30 |
-
31 |
@use
32 |Link an application object to the Bottle app
33 |
34 | -
35 |
@task
36 |Helper that wraps a function inside a separate thread so that 37 | it can execute concurrently.
38 |
39 |
Example
41 |from bottle import route, run
42 | import jyserver.Bottle as js
43 | import time
44 |
45 | @js.use
46 | class App():
47 | def reset(self):
48 | self.start0 = time.time()
49 |
50 | @js.task
51 | def main(self):
52 | self.start0 = time.time()
53 | while True:
54 | t = "{:.1f}".format(time.time() - self.start0)
55 | self.js.dom.time.innerHTML = t
56 | time.sleep(0.1)
57 |
58 | @route('/')
59 | def index():
60 | html = """
61 | <p id="time">WHEN</p>
62 | <button id="b1" onclick="server.reset()">Reset</button>
63 | """
64 | App.main()
65 | return App.render(html)
66 |
67 | run(host='localhost', port=8080)
68 |
69 | 71 | Expand source code 72 |
73 |'''
74 | Module for using jyserver in Bottle. This module provides two new
75 | decorators.
76 |
77 | Decorators
78 | -----------
79 |
80 | * @use
81 |
82 | Link an application object to the Bottle app
83 |
84 | * @task
85 |
86 | Helper that wraps a function inside a separate thread so that
87 | it can execute concurrently.
88 |
89 | Example
90 | -------------
91 | ```python
92 | from bottle import route, run
93 | import jyserver.Bottle as js
94 | import time
95 |
96 | @js.use
97 | class App():
98 | def reset(self):
99 | self.start0 = time.time()
100 |
101 | @js.task
102 | def main(self):
103 | self.start0 = time.time()
104 | while True:
105 | t = "{:.1f}".format(time.time() - self.start0)
106 | self.js.dom.time.innerHTML = t
107 | time.sleep(0.1)
108 |
109 | @route('/')
110 | def index():
111 | html = """
112 | <p id="time">WHEN</p>
113 | <button id="b1" onclick="server.reset()">Reset</button>
114 | """
115 | App.main()
116 | return App.render(html)
117 |
118 | run(host='localhost', port=8080)
119 | ```
120 | '''
121 |
122 | from bottle import route, request
123 |
124 | import json
125 | import jyserver
126 | import threading
127 |
128 | def task(func):
129 | '''
130 | Decorator wraps the function in a separate thread for concurrent
131 | execution.
132 | '''
133 | def wrapper(*args):
134 | server_thread = threading.Thread(target=func, args=(args), daemon=True)
135 | server_thread.start()
136 | return wrapper
137 |
138 | def use(appClass):
139 | '''
140 | Link a class to an app object.
141 | '''
142 | global context
143 | context = jyserver.ClientContext(appClass)
144 |
145 | @route('/_process_srv0', method='POST')
146 | def process():
147 | if request.method == 'POST':
148 | result = context.processCommand(request.json)
149 | if result is None:
150 | return ''
151 | return result
152 | return context
153 | Functions
161 |-
162 |
163 | def task(func) 164 |
165 | -
166 | 168 |
Decorator wraps the function in a separate thread for concurrent 167 | execution.
169 |182 |170 | Expand source code 171 |
172 |
181 |def task(func): 173 | ''' 174 | Decorator wraps the function in a separate thread for concurrent 175 | execution. 176 | ''' 177 | def wrapper(*args): 178 | server_thread = threading.Thread(target=func, args=(args), daemon=True) 179 | server_thread.start() 180 | return wrapper
183 | 184 | def use(appClass) 185 |
186 | -
187 | 188 |
Link a class to an app object.
189 |208 |190 | Expand source code 191 |
192 |
207 |def use(appClass): 193 | ''' 194 | Link a class to an app object. 195 | ''' 196 | global context 197 | context = jyserver.ClientContext(appClass) 198 | 199 | @route('/_process_srv0', method='POST') 200 | def process(): 201 | if request.method == 'POST': 202 | result = context.processCommand(request.json) 203 | if result is None: 204 | return '' 205 | return result 206 | return context
209 |