Skip to main content

SpreadSheet Magic Command

概要

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

使い方

マジックコマンドの追加

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

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

register_smagic()

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

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

%%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内で実行するマジックコマンド

%%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と同じです。

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

参考

とほほのJspreadsheet入門

Release files for spreadmagic 2.6.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 spreadmagic 2.6.1
File Size Uploaded
spreadmagic-2.6.1.tar.gz 4.7 kB Details

Built distribution (wheel)

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

Total release size: 9.8 kB

Release files / spreadmagic-2.6.1.tar.gz

Download URL spreadmagic-2.6.1.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
97b9b912f73181a95f6f6e5d63904cd847c24387ac99bd4af4e55582f8ab1ec4
BLAKE2b-256 checksum
How to use checksums
37b81c68756d69491d12510b97d682399d193686f5caffaff6c59c9be92eeb28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.7

Release files / spreadmagic-2.6.1-py3-none-any.whl

Download URL spreadmagic-2.6.1-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
845259aad7c3ad2abd036127c7e6614a3db8d65513116e6b13f49bee9e97d040
BLAKE2b-256 checksum
How to use checksums
76abe6905b525f4bf7e0b4afbbcad7cdfb83fb7a889fbe9cf6982fc6ef56ce36
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.7

Release history Release notifications | RSS feed

This release

2.6.1 This release

2 release files

2.6.0

2 release files

2.5.0

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

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