Skip to main content

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

Metadata

Release files for moray 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for moray 0.0.1
File Size Uploaded Provenance Actions
moray-0.0.1.tar.gz 17.5 kB Without provenance Details for moray-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for moray 0.0.1
File Interpreter ABI Platform Actions
moray-0.0.1-py3-none-any.whl Python 3 none any Details for moray-0.0.1-py3-none-any.whl

Total release size: 37.3 kB

Release files / moray-0.0.1.tar.gz

Download URL moray-0.0.1.tar.gz
Size 17.5 kB
Tags Source
SHA-256 checksum
How to use checksums
03d5fd0731407d22c9b40727f0ee2a80f8c121d36a5d17de356ab41cf6452387
BLAKE2b-256 checksum
How to use checksums
f2cb2bf1a7a6e88498c6fbc0a74b128e37aaa7a2a75a1df617f6696e791feecc
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / moray-0.0.1-py3-none-any.whl

Download URL moray-0.0.1-py3-none-any.whl
Size 19.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
265f36aa580eb3c4ed06c6ddfc82a204aa0af26d510bd7f1ce8fc43313cb567f
BLAKE2b-256 checksum
How to use checksums
692ce230e691c20f7ccbc6bdcec0c227586d9d2708edfa74ce56704c4be99e4a
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for moray 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page