Examples

Learn by cloning

Snippets mirror the Python entry scripts in the repo (paths trimmed). Open GitHub for the full UI + project files.

01 - hello_world

Minimal two-way hello

Repo: examples/01 - hello_world

import glue

glue.init()

@glue.expose
def say_hello_py(x):
    print('Hello from %s' % x)

say_hello_py('Python World!')
glue.say_hello_js('Python World!')

glue.start()
02 - hello_world_chrome

Force Chrome host

Repo: examples/02 - hello_world_chrome

import glue

glue.init()

@glue.expose
def say_hello_py(x):
    print('Hello from %s' % x)

say_hello_py('Python World!')
glue.say_hello_js('Python World!')

# Force Google Chrome/Chromium (app mode still on)
glue.start(mode='chrome')
03 - callbacks

Async callbacks

Repo: examples/03 - callbacks

import random
import glue

glue.init()

@glue.expose
def py_random():
    return random.random()

@glue.expose
def py_exception(error):
    if error:
        raise ValueError("Test")
    return "No Error"

def print_num(n):
    print('Got this from JavaScript:', n)

def print_num_failed(error, stack):
    print("Error:", error)
    print("Stack:", stack)

glue.js_random()(print_num)
glue.js_random()(lambda n: print('Got this from JavaScript:', n))
glue.js_with_error()(print_num, print_num_failed)

glue.start()
04 - sync_callbacks

Blocking returns

Repo: examples/04 - sync_callbacks

import random
import glue

glue.init()

@glue.expose
def py_random():
    return random.random()

glue.start(block=False)

# Sync calls must happen after start()
n = glue.js_random()()
print('Got this from JavaScript:', n)

while True:
    glue.sleep(1.0)
05 - file_access

Python file access from the UI

Repo: examples/05 - file_access

import os
import random
import glue

glue.init()

@glue.expose
def pick_file(folder):
    folder = os.path.expanduser(folder)
    if os.path.isdir(folder):
        files = [name for name in os.listdir(folder)
                 if os.path.isfile(os.path.join(folder, name))]
        return random.choice(files) if files else 'No files'
    return 'Not a valid folder'

glue.start()
06 - input

Live input bridge

Repo: examples/06 - input

import glue

glue.init()

@glue.expose
def handle_input(value):
    print(value)
    return 'Received by Python: %s' % value

glue.start()
07 - jinja_templates

Jinja2 pages

Repo: examples/07 - jinja_templates

import random
import glue

glue.init()

@glue.expose
def py_random():
    return random.random()

@glue.expose
def say_hello_py(x):
    print('Hello from %s' % x)

say_hello_py('Python World!')
glue.say_hello_js('Python World!')

glue.start(
    'templates/index.html',
    jinja_templates='templates',
)
08 - createreactapp

React + TypeScript

Repo: examples/08 - createreactapp

import os
import random
import sys
import glue

@glue.expose
def say_hello_py(x):
    print('Hello from %s' % x)
    glue.say_hello_js('Python!')

@glue.expose
def pick_file(folder):
    folder = os.path.expanduser(folder)
    if not os.path.isdir(folder):
        return '%s is not a valid folder' % folder
    files = [f for f in os.listdir(folder)
             if not os.path.isdir(os.path.join(folder, f))]
    return random.choice(files) if files else 'No files'

def start_glue(develop):
    if develop:
        glue.init('src', ['.tsx', '.ts', '.jsx', '.js', '.html'])
        page, mode = {'port': 3000}, None
    else:
        glue.init('build', ['.tsx', '.ts', '.jsx', '.js', '.html'])
        page, mode = 'index.html', 'auto'
    glue.start(page, mode=mode, host='localhost', port=8080)

if __name__ == '__main__':
    start_glue(develop=len(sys.argv) == 2)
09 - disable_cache

Cache-Control no-store

Repo: examples/09 - disable_cache

import glue

# Serve ui/ (default). disable_cache defaults to True;
# pass False to let the browser cache UI assets.
glue.init()

glue.start(
    disable_cache=True,
)
10 - custom_app_routes

Custom Bottle routes

Repo: examples/10 - custom_app_routes

import bottle
import glue

app = bottle.Bottle()

@app.route('/custom')
def custom_route():
    return {'message': 'Hello from a custom Bottle route'}

glue.init()

# If wrapping Bottle with middleware, also call:
# glue.register_glue_routes(app)

glue.start(app=app)
11 - splash

Transparent startup splash

Repo: examples/11 - splash

import multiprocessing
import glue

def main():
    glue.init()
    glue.start(splash=True)

if __name__ == '__main__':
    multiprocessing.freeze_support()
    main()
12 - scrolling

Scrolling below window controls

Repo: examples/12 - scrolling

import glue

glue.init()
glue.start()

A naturally overflowing page scrolls beneath the simulated native title bar while its controls remain fixed.