spreadmagic


Namespreadmagic JSON
Version 2.5.0 PyPI version JSON
download
home_pageNone
SummaryThis Python package is a magic command that executes Python code in code cells on Jupyter and Google Colab using PyScript within an iframe.
upload_time2024-11-23 20:26:06
maintainerNone
docs_urlNone
authorUniras
requires_pythonNone
licenseMIT License
keywords pyscript jupyter magic colab iframe jspreadsheet
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # SpreadSheet Magic Command

## 概要

Jypyter(notebook/lab)・VSCodeまたはGoogle Colabでjspresdsheetを使ったコードセルのPythonコードをPyScriptを使ってiframe(ブラウザ)上で実行するマジックコマンドです。

## 使い方

### マジックコマンドの追加

コードセルに以下のコードを貼り付けて実行しマジックコマンドを登録してください。カーネルやランタイムを再起動する度に再実行する必要があります。

```python
%pip install -q -U pysmagic spreadmagic
from spreadmagic import register_smagic

register_smagic()
```

### マジックコマンドの使い方

コードセルの冒頭に以下のようにマジックコマンドを記述してください。実行するとアウトプットにiframeが表示されてその中でコードセルのコードがPyScriptで実行されます。

```python
%%runss 800 400 white

import js

# イベントハンドラのサンプル
def onselection(instance, x1, y1, x2, y2):
    display(f"Selected: {x1}, {y1}, {x2}, {y2}")

# カスタムセル関数のサンプル
def DOUBLE(x):
    return x * 2

# カスタムセル関数の登録
js.DOUBLE = DOUBLE

# jspreadsheet.jsに渡すオプションの設定
options = {
    # データ
    "data": [
        ["2022/10/24", "文房具", 480],
        ["2022/10/26", "外食費", 1390],
        ["2022/10/27", "外食費(2倍)", "=DOUBLE(C2)"],
    ],

    # カラム書式の定義
    "columns": [
        { "type": "calendar", "title": "日付", "width": 120, "options": { "format": "YYYY/MM/DD" } },
        { "type": "text", "title": "項目", "width": 300 },
        { "type": "numeric", "title": "出金", "width": 200, "mask":"#,##" },
    ],

    # イベントハンドラの登録
    "onselection": onselection,
}
```

### グローバル変数

PyScriptから以下の変数にアクセスできます。

- 別のセルで設定したグローバル変数(_で始まる変数名やJSONに変換できないものは除く)
- マジックコマンドの引数py_valで設定した変数
- width: iframeの幅(マジックコマンドの引数で指定した幅)
- height: iframeの高さ(マジックコマンドの引数で指定した高さ)

この変数はjs.pysオブジェクトを介してアクセスできます。
変数名が衝突した場合は上記リストの順に上書きされて適用されます。

### マジックコマンド

#### %%runss

セル内のjspreadsheet.jsを使ったPythonコードをPyScriptを用いてiframe内で実行するマジックコマンド

```juypyter
%%runss [width] [height] [background] [py_type] [py_val] [py_conf] [js_src] [py_ver]
```

- width: iframeの幅を指定します。デフォルトは500です。
- height: iframeの高さを指定します。デフォルトは500です。
- background: iframeの背景色を指定します。デフォルトはwhiteです。
- py_type: 実行するPythonの種類。pyまたはmpyを指定します。mpyはMicroPyton、pyはCPython互換のPyodideで実行します。デフォルトはmpyです。グローバルモードのときはmpy固定です。
- py_val: PyScriptの変数を''で囲んだJSON形式で指定します。デフォルトは'{}'です。
- py_conf: PyScriptの設定を''で囲んだJSON形式で指定します。デフォルトは{}です。
- js_src: 外部JavaScriptのURLを''で囲んだ文字列のJSON配列形式で指定します。デフォルトは[]です。
- py_ver: PyScriptのバージョンを指定します.

#### %%genss

セル内のjspreadsheet.jsを使ったPythonコードをPythonコードからブラウザで実行可能な単一HTMLを生成するマジックコマンド。オプションはrunssと同じです。

```juypyter
%%genss [width] [height] [background] [py_type] [py_conf] [js_src] [py_ver]
```

## 参考

[とほほのJspreadsheet入門](https://www.tohoho-web.com/ex/jspreadsheet.html)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "spreadmagic",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "PyScript, jupyter, magic, colab, iframe, jspreadsheet",
    "author": "Uniras",
    "author_email": "tkappeng@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/98/68/81d72a569d69a824880c05c45ed27689d02d0ba6c7c1d3d5b6aa725589fd/spreadmagic-2.5.0.tar.gz",
    "platform": null,
    "description": "# SpreadSheet Magic Command\r\n\r\n## \u6982\u8981\r\n\r\nJypyter(notebook/lab)\u30fbVSCode\u307e\u305f\u306fGoogle Colab\u3067jspresdsheet\u3092\u4f7f\u3063\u305f\u30b3\u30fc\u30c9\u30bb\u30eb\u306ePython\u30b3\u30fc\u30c9\u3092PyScript\u3092\u4f7f\u3063\u3066iframe(\u30d6\u30e9\u30a6\u30b6)\u4e0a\u3067\u5b9f\u884c\u3059\u308b\u30de\u30b8\u30c3\u30af\u30b3\u30de\u30f3\u30c9\u3067\u3059\u3002\r\n\r\n## \u4f7f\u3044\u65b9\r\n\r\n### \u30de\u30b8\u30c3\u30af\u30b3\u30de\u30f3\u30c9\u306e\u8ffd\u52a0\r\n\r\n\u30b3\u30fc\u30c9\u30bb\u30eb\u306b\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u8cbc\u308a\u4ed8\u3051\u3066\u5b9f\u884c\u3057\u30de\u30b8\u30c3\u30af\u30b3\u30de\u30f3\u30c9\u3092\u767b\u9332\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u30ab\u30fc\u30cd\u30eb\u3084\u30e9\u30f3\u30bf\u30a4\u30e0\u3092\u518d\u8d77\u52d5\u3059\u308b\u5ea6\u306b\u518d\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\r\n\r\n```python\r\n%pip install -q -U pysmagic spreadmagic\r\nfrom spreadmagic import register_smagic\r\n\r\nregister_smagic()\r\n```\r\n\r\n### \u30de\u30b8\u30c3\u30af\u30b3\u30de\u30f3\u30c9\u306e\u4f7f\u3044\u65b9\r\n\r\n\u30b3\u30fc\u30c9\u30bb\u30eb\u306e\u5192\u982d\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30de\u30b8\u30c3\u30af\u30b3\u30de\u30f3\u30c9\u3092\u8a18\u8ff0\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u5b9f\u884c\u3059\u308b\u3068\u30a2\u30a6\u30c8\u30d7\u30c3\u30c8\u306biframe\u304c\u8868\u793a\u3055\u308c\u3066\u305d\u306e\u4e2d\u3067\u30b3\u30fc\u30c9\u30bb\u30eb\u306e\u30b3\u30fc\u30c9\u304cPyScript\u3067\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002\r\n\r\n```python\r\n%%runss 800 400 white\r\n\r\nimport js\r\n\r\n# \u30a4\u30d9\u30f3\u30c8\u30cf\u30f3\u30c9\u30e9\u306e\u30b5\u30f3\u30d7\u30eb\r\ndef onselection(instance, x1, y1, x2, y2):\r\n    display(f\"Selected: {x1}, {y1}, {x2}, {y2}\")\r\n\r\n# \u30ab\u30b9\u30bf\u30e0\u30bb\u30eb\u95a2\u6570\u306e\u30b5\u30f3\u30d7\u30eb\r\ndef DOUBLE(x):\r\n    return x * 2\r\n\r\n# \u30ab\u30b9\u30bf\u30e0\u30bb\u30eb\u95a2\u6570\u306e\u767b\u9332\r\njs.DOUBLE = DOUBLE\r\n\r\n# jspreadsheet.js\u306b\u6e21\u3059\u30aa\u30d7\u30b7\u30e7\u30f3\u306e\u8a2d\u5b9a\r\noptions = {\r\n    # \u30c7\u30fc\u30bf\r\n    \"data\": [\r\n        [\"2022/10/24\", \"\u6587\u623f\u5177\", 480],\r\n        [\"2022/10/26\", \"\u5916\u98df\u8cbb\", 1390],\r\n        [\"2022/10/27\", \"\u5916\u98df\u8cbb(2\u500d)\", \"=DOUBLE(C2)\"],\r\n    ],\r\n\r\n    # \u30ab\u30e9\u30e0\u66f8\u5f0f\u306e\u5b9a\u7fa9\r\n    \"columns\": [\r\n        { \"type\": \"calendar\", \"title\": \"\u65e5\u4ed8\", \"width\": 120, \"options\": { \"format\": \"YYYY/MM/DD\" } },\r\n        { \"type\": \"text\", \"title\": \"\u9805\u76ee\", \"width\": 300 },\r\n        { \"type\": \"numeric\", \"title\": \"\u51fa\u91d1\", \"width\": 200, \"mask\":\"#,##\" },\r\n    ],\r\n\r\n    # \u30a4\u30d9\u30f3\u30c8\u30cf\u30f3\u30c9\u30e9\u306e\u767b\u9332\r\n    \"onselection\": onselection,\r\n}\r\n```\r\n\r\n### \u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\r\n\r\nPyScript\u304b\u3089\u4ee5\u4e0b\u306e\u5909\u6570\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u3059\u3002\r\n\r\n- \u5225\u306e\u30bb\u30eb\u3067\u8a2d\u5b9a\u3057\u305f\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570(_\u3067\u59cb\u307e\u308b\u5909\u6570\u540d\u3084JSON\u306b\u5909\u63db\u3067\u304d\u306a\u3044\u3082\u306e\u306f\u9664\u304f)\r\n- \u30de\u30b8\u30c3\u30af\u30b3\u30de\u30f3\u30c9\u306e\u5f15\u6570py_val\u3067\u8a2d\u5b9a\u3057\u305f\u5909\u6570\r\n- width: iframe\u306e\u5e45(\u30de\u30b8\u30c3\u30af\u30b3\u30de\u30f3\u30c9\u306e\u5f15\u6570\u3067\u6307\u5b9a\u3057\u305f\u5e45)\r\n- height: iframe\u306e\u9ad8\u3055(\u30de\u30b8\u30c3\u30af\u30b3\u30de\u30f3\u30c9\u306e\u5f15\u6570\u3067\u6307\u5b9a\u3057\u305f\u9ad8\u3055)\r\n\r\n\u3053\u306e\u5909\u6570\u306fjs.pys\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4ecb\u3057\u3066\u30a2\u30af\u30bb\u30b9\u3067\u304d\u307e\u3059\u3002\r\n\u5909\u6570\u540d\u304c\u885d\u7a81\u3057\u305f\u5834\u5408\u306f\u4e0a\u8a18\u30ea\u30b9\u30c8\u306e\u9806\u306b\u4e0a\u66f8\u304d\u3055\u308c\u3066\u9069\u7528\u3055\u308c\u307e\u3059\u3002\r\n\r\n### \u30de\u30b8\u30c3\u30af\u30b3\u30de\u30f3\u30c9\r\n\r\n#### %%runss\r\n\r\n\u30bb\u30eb\u5185\u306ejspreadsheet.js\u3092\u4f7f\u3063\u305fPython\u30b3\u30fc\u30c9\u3092PyScript\u3092\u7528\u3044\u3066iframe\u5185\u3067\u5b9f\u884c\u3059\u308b\u30de\u30b8\u30c3\u30af\u30b3\u30de\u30f3\u30c9\r\n\r\n```juypyter\r\n%%runss [width] [height] [background] [py_type] [py_val] [py_conf] [js_src] [py_ver]\r\n```\r\n\r\n- width: iframe\u306e\u5e45\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u306f500\u3067\u3059\u3002\r\n- height: iframe\u306e\u9ad8\u3055\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u306f500\u3067\u3059\u3002\r\n- background: iframe\u306e\u80cc\u666f\u8272\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u306fwhite\u3067\u3059\u3002\r\n- py_type: \u5b9f\u884c\u3059\u308bPython\u306e\u7a2e\u985e\u3002py\u307e\u305f\u306fmpy\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002mpy\u306fMicroPyton\u3001py\u306fCPython\u4e92\u63db\u306ePyodide\u3067\u5b9f\u884c\u3057\u307e\u3059\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u306fmpy\u3067\u3059\u3002\u30b0\u30ed\u30fc\u30d0\u30eb\u30e2\u30fc\u30c9\u306e\u3068\u304d\u306fmpy\u56fa\u5b9a\u3067\u3059\u3002\r\n- py_val: PyScript\u306e\u5909\u6570\u3092''\u3067\u56f2\u3093\u3060JSON\u5f62\u5f0f\u3067\u6307\u5b9a\u3057\u307e\u3059\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u306f'{}'\u3067\u3059\u3002\r\n- py_conf: PyScript\u306e\u8a2d\u5b9a\u3092''\u3067\u56f2\u3093\u3060JSON\u5f62\u5f0f\u3067\u6307\u5b9a\u3057\u307e\u3059\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u306f{}\u3067\u3059\u3002\r\n- js_src: \u5916\u90e8JavaScript\u306eURL\u3092''\u3067\u56f2\u3093\u3060\u6587\u5b57\u5217\u306eJSON\u914d\u5217\u5f62\u5f0f\u3067\u6307\u5b9a\u3057\u307e\u3059\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u306f[]\u3067\u3059\u3002\r\n- py_ver: PyScript\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u6307\u5b9a\u3057\u307e\u3059.\r\n\r\n#### %%genss\r\n\r\n\u30bb\u30eb\u5185\u306ejspreadsheet.js\u3092\u4f7f\u3063\u305fPython\u30b3\u30fc\u30c9\u3092Python\u30b3\u30fc\u30c9\u304b\u3089\u30d6\u30e9\u30a6\u30b6\u3067\u5b9f\u884c\u53ef\u80fd\u306a\u5358\u4e00HTML\u3092\u751f\u6210\u3059\u308b\u30de\u30b8\u30c3\u30af\u30b3\u30de\u30f3\u30c9\u3002\u30aa\u30d7\u30b7\u30e7\u30f3\u306frunss\u3068\u540c\u3058\u3067\u3059\u3002\r\n\r\n```juypyter\r\n%%genss [width] [height] [background] [py_type] [py_conf] [js_src] [py_ver]\r\n```\r\n\r\n## \u53c2\u8003\r\n\r\n[\u3068\u307b\u307b\u306eJspreadsheet\u5165\u9580](https://www.tohoho-web.com/ex/jspreadsheet.html)\r\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "This Python package is a magic command that executes Python code in code cells on Jupyter and Google Colab using PyScript within an iframe.",
    "version": "2.5.0",
    "project_urls": {
        "Homepage": "https://github.com/uniras/SpreadMagic",
        "Repository": "https://github.com/uniras/SpreadMagic"
    },
    "split_keywords": [
        "pyscript",
        " jupyter",
        " magic",
        " colab",
        " iframe",
        " jspreadsheet"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a633a769fa3f03f2c60f40d475b59cadf045d07242bbd34fcd920df6e6685b79",
                "md5": "fc116fce30afb3d0367cfbf8a005fd13",
                "sha256": "796f928df34154a9cc8c2f6df6ba8a05ea4ff3237eab635e28c058bb6f2a024a"
            },
            "downloads": -1,
            "filename": "spreadmagic-2.5.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "fc116fce30afb3d0367cfbf8a005fd13",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 4898,
            "upload_time": "2024-11-23T20:26:04",
            "upload_time_iso_8601": "2024-11-23T20:26:04.922929Z",
            "url": "https://files.pythonhosted.org/packages/a6/33/a769fa3f03f2c60f40d475b59cadf045d07242bbd34fcd920df6e6685b79/spreadmagic-2.5.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "986881d72a569d69a824880c05c45ed27689d02d0ba6c7c1d3d5b6aa725589fd",
                "md5": "75c6a9942a31a5bb6aec08558bd41b35",
                "sha256": "f6659ee83fb80dd1d41a91247f1dfd2c4bef9fa49ff08f70d15106a95fd48d6e"
            },
            "downloads": -1,
            "filename": "spreadmagic-2.5.0.tar.gz",
            "has_sig": false,
            "md5_digest": "75c6a9942a31a5bb6aec08558bd41b35",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4404,
            "upload_time": "2024-11-23T20:26:06",
            "upload_time_iso_8601": "2024-11-23T20:26:06.621530Z",
            "url": "https://files.pythonhosted.org/packages/98/68/81d72a569d69a824880c05c45ed27689d02d0ba6c7c1d3d5b6aa725589fd/spreadmagic-2.5.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-23 20:26:06",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "uniras",
    "github_project": "SpreadMagic",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "spreadmagic"
}
        
Elapsed time: 0.51614s