redirect.pyΒΆ

open in new tab
 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
"""
Example that demonstrates redirecting and opening a new page.
This example only works when opened in a browser (not with
``launch(Redirect, 'app')``).

Note that one can also open a webpage from Python:

import webbrowser
webbrowser.open('http://python.org')
"""

from flexx import flx


class Redirect(flx.Widget):

    def init(self):
        self.but1 = flx.Button(text='Redirect')
        self.but2 = flx.Button(text='Open new page')

    @flx.reaction('but1.pointer_click')
    def on_redirect(self, *events):
        global window
        window.location.href = 'http://python.org'  # allow going back
        # window.location.replace('http://python.org')  # hard redirect

    @flx.reaction('but2.pointer_click')
    def on_opennew(self, *events):
        global window
        window.open('http://python.org', '_blank')


if __name__ == '__main__':
    m = flx.launch(Redirect, 'browser')
    flx.start()