pysmagic


Namepysmagic JSON
Version 1.0.2 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-06-16 13:22:51
maintainerNone
docs_urlNone
authorUniras
requires_pythonNone
licenseMIT License
keywords pyscript jupyter magic
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # PyScript Magic Command

## 概要

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

## 使い方

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

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

```python
%pip install pysmagic
from pysmagic import register_pysmagic

register_pysmagic()
```

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

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

```python
%%run_iframe mpy 500 500 white '{}' '[]'

from pyscript import display
display("Hello, world!")
```

以下はブラウザ用JavaScriptライブラリのp5.jsを使って円を描画し、キーボードの矢印キーで移動させるサンプルです。

```python
%%run_iframe mpy 500 500 white '{}' '["https://cdn.jsdelivr.net/npm/p5@1.9.4/lib/p5.js"]'

import pyscript
import js

def start(p5):
    x = 100
    y = 100

    def setup():
        p5.createCanvas(300, 300)

    def draw():
        nonlocal x, y
        p5.background(128)
        p5.fill(255, 0, 0)
        p5.ellipse(x, y, 50, 50)

        if p5.keyIsDown(p5.LEFT_ARROW):
            x -= 1
        if p5.keyIsDown(p5.RIGHT_ARROW):
            x += 1
        if p5.keyIsDown(p5.UP_ARROW):
            y -= 1
        if p5.keyIsDown(p5.DOWN_ARROW):
            y += 1

    p5.setup = setup
    p5.draw = draw

p5run = js.window.Function("func", "new p5((p) => {func(p)})")
p5run(start)
```

### マジックコマンド

#### %%run_iframe

コードセルのコードをPyScriptを使ってiframe内で実行します。

```jupyter
%%run_iframe [type] [width] [height] [background] [py_conf] [js_src] [version]
```

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

#### %%view_iframe

セル内のPythonコードをPyScriptを用いてiframe内で実行するために生成したHTMLを表示するマジックコマンド

引数は%%run_iframeと同じです。

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pysmagic",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "PyScript, jupyter, magic",
    "author": "Uniras",
    "author_email": "tkappeng@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/32/e4/8b1bb20b433a71b3e4d309d933ed1a8d5874372f1d7f34fd86c3e464f7d6/pysmagic-1.0.2.tar.gz",
    "platform": null,
    "description": "# PyScript Magic Command\r\n\r\n## \u6982\u8981\r\n\r\nJypyter(notebook/lab)\u30fbVSCode\u307e\u305f\u306fGoogle Colab\u3067\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 pysmagic\r\nfrom pysmagic import register_pysmagic\r\n\r\nregister_pysmagic()\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%%run_iframe mpy 500 500 white '{}' '[]'\r\n\r\nfrom pyscript import display\r\ndisplay(\"Hello, world!\")\r\n```\r\n\r\n\u4ee5\u4e0b\u306f\u30d6\u30e9\u30a6\u30b6\u7528JavaScript\u30e9\u30a4\u30d6\u30e9\u30ea\u306ep5.js\u3092\u4f7f\u3063\u3066\u5186\u3092\u63cf\u753b\u3057\u3001\u30ad\u30fc\u30dc\u30fc\u30c9\u306e\u77e2\u5370\u30ad\u30fc\u3067\u79fb\u52d5\u3055\u305b\u308b\u30b5\u30f3\u30d7\u30eb\u3067\u3059\u3002\r\n\r\n```python\r\n%%run_iframe mpy 500 500 white '{}' '[\"https://cdn.jsdelivr.net/npm/p5@1.9.4/lib/p5.js\"]'\r\n\r\nimport pyscript\r\nimport js\r\n\r\ndef start(p5):\r\n    x = 100\r\n    y = 100\r\n\r\n    def setup():\r\n        p5.createCanvas(300, 300)\r\n\r\n    def draw():\r\n        nonlocal x, y\r\n        p5.background(128)\r\n        p5.fill(255, 0, 0)\r\n        p5.ellipse(x, y, 50, 50)\r\n\r\n        if p5.keyIsDown(p5.LEFT_ARROW):\r\n            x -= 1\r\n        if p5.keyIsDown(p5.RIGHT_ARROW):\r\n            x += 1\r\n        if p5.keyIsDown(p5.UP_ARROW):\r\n            y -= 1\r\n        if p5.keyIsDown(p5.DOWN_ARROW):\r\n            y += 1\r\n\r\n    p5.setup = setup\r\n    p5.draw = draw\r\n\r\np5run = js.window.Function(\"func\", \"new p5((p) => {func(p)})\")\r\np5run(start)\r\n```\r\n\r\n### \u30de\u30b8\u30c3\u30af\u30b3\u30de\u30f3\u30c9\r\n\r\n#### %%run_iframe\r\n\r\n\u30b3\u30fc\u30c9\u30bb\u30eb\u306e\u30b3\u30fc\u30c9\u3092PyScript\u3092\u4f7f\u3063\u3066iframe\u5185\u3067\u5b9f\u884c\u3057\u307e\u3059\u3002\r\n\r\n```jupyter\r\n%%run_iframe [type] [width] [height] [background] [py_conf] [js_src] [version]\r\n```\r\n\r\n- type: \u5b9f\u884c\u3059\u308bPython\u306e\u7a2e\u985e\u3002py\u307e\u305f\u306fmpy\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002py\u306f CPython\u4e92\u63db\u306ePyodide\u3001mpy\u306fMicroPyton\u3067\u5b9f\u884c\u3057\u307e\u3059\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u306fmpy\u3067\u3059\u3002\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_conf: PyScript\u306e\u8a2d\u5b9a\u3092''\u3067\u56f2\u3093\u3060JSON\u6587\u5b57\u5217\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- version: PyScript\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u6307\u5b9a\u3057\u307e\u3059\r\n\r\n#### %%view_iframe\r\n\r\n\u30bb\u30eb\u5185\u306ePython\u30b3\u30fc\u30c9\u3092PyScript\u3092\u7528\u3044\u3066iframe\u5185\u3067\u5b9f\u884c\u3059\u308b\u305f\u3081\u306b\u751f\u6210\u3057\u305fHTML\u3092\u8868\u793a\u3059\u308b\u30de\u30b8\u30c3\u30af\u30b3\u30de\u30f3\u30c9\r\n\r\n\u5f15\u6570\u306f%%run_iframe\u3068\u540c\u3058\u3067\u3059\u3002\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": "1.0.2",
    "project_urls": {
        "Homepage": "https://github.com/uniras/PyScriptMagic",
        "Repository": "https://github.com/uniras/PyScriptMagic"
    },
    "split_keywords": [
        "pyscript",
        " jupyter",
        " magic"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e25bf153b5f36ff95402e06c686b088069e7fdbb30115a35026c72e92fdbeee1",
                "md5": "e46c8b719a44a140f1d1b619ec7aac18",
                "sha256": "10bd8040cc458c566f94af04d70dc6bd1205c93139feef3b78fb1ff9e5d9cf6b"
            },
            "downloads": -1,
            "filename": "pysmagic-1.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e46c8b719a44a140f1d1b619ec7aac18",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 6489,
            "upload_time": "2024-06-16T13:22:50",
            "upload_time_iso_8601": "2024-06-16T13:22:50.365135Z",
            "url": "https://files.pythonhosted.org/packages/e2/5b/f153b5f36ff95402e06c686b088069e7fdbb30115a35026c72e92fdbeee1/pysmagic-1.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "32e48b1bb20b433a71b3e4d309d933ed1a8d5874372f1d7f34fd86c3e464f7d6",
                "md5": "5b7f72130601427195fac9e231e77740",
                "sha256": "b961e82fac1bd070b520cb88b8061225e06b10be6f50c46ee9039c1047e255d8"
            },
            "downloads": -1,
            "filename": "pysmagic-1.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "5b7f72130601427195fac9e231e77740",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5928,
            "upload_time": "2024-06-16T13:22:51",
            "upload_time_iso_8601": "2024-06-16T13:22:51.930031Z",
            "url": "https://files.pythonhosted.org/packages/32/e4/8b1bb20b433a71b3e4d309d933ed1a8d5874372f1d7f34fd86c3e464f7d6/pysmagic-1.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-16 13:22:51",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "uniras",
    "github_project": "PyScriptMagic",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pysmagic"
}
        
Elapsed time: 0.51113s