Skip to main content

This Python package is a magic command that executes Python code in code cells on Jupyter and Google Colab using PyScript within an iframe.

Project description

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入門

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

spreadmagic-2.6.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

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

spreadmagic-2.6.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file spreadmagic-2.6.1.tar.gz.

File metadata

  • Download URL: spreadmagic-2.6.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for spreadmagic-2.6.1.tar.gz
Algorithm Hash digest
SHA256 97b9b912f73181a95f6f6e5d63904cd847c24387ac99bd4af4e55582f8ab1ec4
MD5 20e8a88148baa494d8eb4726944760c7
BLAKE2b-256 37b81c68756d69491d12510b97d682399d193686f5caffaff6c59c9be92eeb28

See more details on using hashes here.

File details

Details for the file spreadmagic-2.6.1-py3-none-any.whl.

File metadata

  • Download URL: spreadmagic-2.6.1-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for spreadmagic-2.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 845259aad7c3ad2abd036127c7e6614a3db8d65513116e6b13f49bee9e97d040
MD5 eeb40f64e65dd21cf6787a2de5666402
BLAKE2b-256 76abe6905b525f4bf7e0b4afbbcad7cdfb83fb7a889fbe9cf6982fc6ef56ce36

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