Skip to main content

Package for creating HTML GUI using Python modules and JavaScript.

Project description

moray

README.md is a translation of README_jp.md.
So, there may be a mistranslation.

  • Package for creating HTML GUI using Python modules and JavaScript.
  • Managing Python functions in modules.

Contents


Install

  • Execute the following command.
    pip install moray
    

Directory Structure

  • The moray application consists of a front-end with .html, .js, .css, etc., and a back-end with Python scripts.
  • /moray.js and /moray/ cannot be used, because moray use them.
    python_script.py     <-- Python script
    web/                 <-- static web directory
      index.html
      css/
        style.css
      img/
        logo.png
    

Usage

Starting the app

  • Suppose you put all the frontend files in web directory, including your start page index.html, then the app is started like this.
    import moray
    
    moray.run('web')
    
    • This will open a browser to http://localhost:<automatically picked port>/index.html.

App options

  • Additional options can be passed to moray.run() as keyword arguments.

    • start_page
      • str type (Default: '')
      • Your start page.
        • If '', index.html will be opened.
    • host
      • str type (Default: 'localhost')
      • Hostname to use for the Bottle server.
        • 'localhost' or IP address is allowed.
    • port
      • int type (Default: 0)
      • Port to use for the Bottle server.
        • 0 or a value between 1025 and 65535 is allowed.
    • browser
      • str type (Default: 'chrome')
      • Browser to use.
        • Only 'chrome' can be used.
    • cmdline_args
      • list of str type (Default: [])
      • Command line arguments to start the browser.
    • position
      • tuple of 2 int type (Default: None)
      • The (left, top) of the main window in pixels.
        • If not specified, None.
    • size
      • tuple of 2 int type (Default: None)
      • The (width, height) of the main window in pixels.
        • If not specified, None.
  • Example

    import moray
    
    moray.run(
        'web',
        start_page = 'index.html',
        host = 'localhost',
        port = 8000,
        browser = 'chrome',
        cmdline_args = ['--disable-http-cache', '--incognito'],
        position = (400, 200),
        size = (800, 600)
    )
    

Call Python from JavaScript

  • New thread is created for each Python call.
  • py_module.py
    import moray
    
    # Expose by decorator so that it can be called from JavaScript.
    @moray.expose
    def py_func(arg):
        return 'result'
    
  • js_module.js
    // Import exposed Python function.
    // import {<function name>} from '/moray/py/<module name>.js'
    import {py_func} from '/moray/py/py_module.js'
    
    // Call Python function.
    py_func('arg')
    
    // To get the return value,
    // use "then" and "catch" of Promise object.
    py_func('arg').then(
        // Execution result is returned.
        v => ・・・
    ).catch(
        // Exception message is returned.
        v => ・・・
    )
    

Call JavaScript from Python

  • If a Python function is called within a JavaScript function, it will be in a different thread than the calling Python function.
  • js_module.js
    import moray from '/moray.js'
    import {py_func} from '/moray/py/py_module.js'
    
    // Expose so that it can be called from Python.
    const js_func(arg) = function() {
        return 'result'
    }
    moray.expose(js_func)
    
    // Call Python function.
    py_func()
    
  • py_module.py
    import moray
    
    @moray.expose
    def py_func():
        
        # Call JavaScript function.
        moray.js.js_func('arg')
        
        try:
            # To get the return value,
            # use "moray.js.<function name>(<arguments>)()".
            result = moray.js.js_func('arg')()
        except Exception as e:
            # When an exception is raised in JavaScript.
            print(e)
    

Abnormal exit handler

  • "moray.onclose" is handler that moray end.
  • js_module.js
    import moray from '/moray.js'
    
    // Define a function in "moray.onclose".
    // "evt" is null.
    moray.onclose = function(evt) {
        alert('moray closed');
    }
    

Logging

  • moray uses logging module to logging.
  • So you can set up a logger for "moray" to logging.
  • Example
    import logging
    
    format = '[%(asctime)s][%(levelname)s] %(message)s (at %(name)s:%(lineno)s)'
    formatter = logging.Formatter(format)
    
    handler = logging.StreamHandler()
    handler.setLevel(logging.DEBUG)
    handler.setFormatter(formatter)
    
    logger = logging.getLogger('moray')
    logger.addHandler(handler)
    logger.setLevel(logging.INFO)
    

Packages using

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

moray-0.0.1.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

moray-0.0.1-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file moray-0.0.1.tar.gz.

File metadata

  • Download URL: moray-0.0.1.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.2

File hashes

Hashes for moray-0.0.1.tar.gz
Algorithm Hash digest
SHA256 03d5fd0731407d22c9b40727f0ee2a80f8c121d36a5d17de356ab41cf6452387
MD5 28cf5bdc3b1d43aa4ee8e9d6f8f2b994
BLAKE2b-256 f2cb2bf1a7a6e88498c6fbc0a74b128e37aaa7a2a75a1df617f6696e791feecc

See more details on using hashes here.

File details

Details for the file moray-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: moray-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.2

File hashes

Hashes for moray-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 265f36aa580eb3c4ed06c6ddfc82a204aa0af26d510bd7f1ce8fc43313cb567f
MD5 90bf28bd7d4fef84092cfabdcecd1f5e
BLAKE2b-256 692ce230e691c20f7ccbc6bdcec0c227586d9d2708edfa74ce56704c4be99e4a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page