RunServer Magic Command
概要
Jypyter(notebook/lab)・VSCodeまたはGoogle ColabでコードセルのPythonコードをサーバーとして起動するマジックコマンドです。
使い方
マジックコマンドの追加
コードセルに以下のコードを貼り付けて実行しマジックコマンドを登録してください。カーネルやランタイムを再起動する度に再実行する必要があります。
%pip install -q runservermagic
from runserver import register_run_server
register_run_server()
マジックコマンドの使い方
コードセルの冒頭に%%runservマジックコマンドを記述してください。実行するとコードセルのコードがサーバーとして実行されます。
以下はFlaskでHello Worldを表示するサーバーの例です。
実行するとサーバーを起動し、IFrameでサーバーにアクセスし出力されたwebページを表示します。
%%runserv 8000
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
app.run(port={{server_port}})
{{server_port}} はマジックコマンドの引数で指定したポート番号に置き換えられます。
起動したサーバーを停止するには%endservマジックコマンドを別のコードセルから実行してください。
同じポート番号でサーバーを起動した場合は、自動的に古いサーバーを停止してから新しいサーバーを起動します。
別のセルからIPython.display.IFrameを使用してwebページを表示することもできます。server_urlには直前に%%runservで起動したサーバーのURLが格納されています。
import IPython
IPython.display.IFrame(src=f"{server_url}/", width="100%", height=500) # type: ignore
マジックコマンド
%%runserv
セル内のPythonコードをサーバーとして起動します。
%%runserv [port] [show_iframe] [width] [height]
port: サーバーのポート番号を指定します。デフォルトは8000です。show_iframe: 起動したサーバーにアクセスするIFrameを表示するかどうかを指定します。デフォルトはFalseです。width: IFrameの幅を指定します。デフォルトは500です。height: IFrameの高さを指定します。デフォルトは500です。
%endserv
起動したサーバーを停止します。
%endserv [port]
port: 停止するサーバーのポート番号を指定します。デフォルトは8000です。
グローバル変数
run_serverマジックコマンドがあるセルを実行した後には以下のグローバル変数が登録されています
server_url: 直前に起動したサーバーのURLが格納されています。server_url_{port番号}: 指定のポート番号で起動したサーバーのURLが格納されています。
Metadata
Release files for runservermagic 3.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| runservermagic-3.0.1.tar.gz | 4.5 kB | Without provenance | Details for runservermagic-3.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| runservermagic-3.0.1-py3-none-any.whl | Python 3 | none | any | Details for runservermagic-3.0.1-py3-none-any.whl |
Total release size: 9.6 kB
Release files / runservermagic-3.0.1.tar.gz
| Download URL | runservermagic-3.0.1.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
73c946fbc2a737e1f6ddaaf255787cfdabee6a453ea4560f5d3ca8f61cca452c
|
|
BLAKE2b-256 checksum How to use checksums |
2e8ef94f77100730b5332506dd31c1fb5c5e334868cd64232456c731cd828a94
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.13.0
|
Release files / runservermagic-3.0.1-py3-none-any.whl
| Download URL | runservermagic-3.0.1-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
65a5333d94da2572fc27dfde4b0fa6f77b3be6162b212fdafbb990b3715966fe
|
|
BLAKE2b-256 checksum How to use checksums |
0d9d4d334a790aa32dbbb0aef3ebb2741d518b5d79122bb3d0da121c10dfbecb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.13.0
|
Security for runservermagic 3.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.