|
| 1 | +""" |
| 2 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + you may not use this file except in compliance with the License. |
| 4 | + You may obtain a copy of the License at |
| 5 | +
|
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | + Unless required by applicable law or agreed to in writing, software |
| 9 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + See the License for the specific language governing permissions and |
| 12 | + limitations under the License. |
| 13 | +""" |
| 14 | + |
| 15 | +""" |
| 16 | + This example shows how to start the application as a thread, |
| 17 | + without stopping the main thread. |
| 18 | + A label is accessed from the main thread. |
| 19 | + NOTE: |
| 20 | + It is important to run the server with parameter multiple_instance=False |
| 21 | +""" |
| 22 | + |
| 23 | +import remi |
| 24 | +import remi.gui as gui |
| 25 | +from remi import start, App, Server |
| 26 | +import time |
| 27 | + |
| 28 | +#here will be stored the application instance once a client connects to. |
| 29 | +global_app_instance = None |
| 30 | + |
| 31 | +class MyApp(App): |
| 32 | + label = None |
| 33 | + |
| 34 | + def main(self): |
| 35 | + global global_app_instance |
| 36 | + global_app_instance = self |
| 37 | + |
| 38 | + # creating a container VBox type, vertical (you can use also HBox or Widget) |
| 39 | + main_container = gui.VBox(width=300, height=200, style={'margin': '0px auto'}) |
| 40 | + self.label = gui.Label("a label") |
| 41 | + |
| 42 | + main_container.append(self.label) |
| 43 | + |
| 44 | + # returning the root widget |
| 45 | + return main_container |
| 46 | + |
| 47 | + |
| 48 | +if __name__ == "__main__": |
| 49 | + #create the server with parameter start=False to prevent server autostart |
| 50 | + server = remi.Server(MyApp, start=False, address='0.0.0.0', port=0, \ |
| 51 | + start_browser=True, multiple_instance=False) |
| 52 | + #start the server programmatically |
| 53 | + server.start() |
| 54 | + |
| 55 | + index = 0 |
| 56 | + #loop the main thread |
| 57 | + while True: |
| 58 | + #checks that the app instance is created |
| 59 | + if not global_app_instance is None: |
| 60 | + #the update lock is important to sync the app thread |
| 61 | + with global_app_instance.update_lock: |
| 62 | + #set the label value |
| 63 | + global_app_instance.label.set_text("%s"%index) |
| 64 | + index = index + 1 |
| 65 | + time.sleep(1) |
0 commit comments