QuartoのTips (Python版)

Quartoを使うときのTipsのうち、Python (Jupyter)特有の設定などについて紹介する。
Jupyter
Python
Quarto
Published

July 24, 2024

Modified

September 17, 2024

導入

まずはRyeを使ってプロジェクト内にPython仮想環境を作成し、必要な依存ライブラリをインストールしておく。 Pythonの仮想環境の構築、Ryeの使い方などは以下の記事に簡単にまとめた。

Pythonのプロジェクトの始め方 – めも

Pythonでプロジェクト管理を行う方法について調べたことをまとめた

https://t-arae.blog/posts/2024/2024-02-15-python-hello-world/

RStudioを使って.qmdファイルを書く場合は、仮想環境を作成したプロジェクトを開き、 メニューのTools/Project Options...から、Pythonタブを開き、Select...ボタンを押して、Virtual Environmentsから作成したvenv内のpythonを選択する。

必要なパッケージが見つからない旨のエラーが出た場合は、一度RStudioを再起動する。

コードチャンクのオプション

QuartoでPythonのコードチャンク(だけ)を書くと、 コードを実行する実行エンジンはデフォルトではJupyterが選択される。 Jupyter特有のオプションは公式ドキュメントのJupyter Optionsを参照すると良い。

以下ではいくつか紹介する。

Figure options

Jupyter特有のオプションではないが、Knitrとの相違点があるオプションとして、 Knitrではfig-widthfig-heightはチャンクごとに設定できるが、 Jupyterでは最低でもドキュメントレベルでしか設定できない。

When using the Knitr engine, fig-width and fig-height are supported on a per-cell basis. But when using the Jupyter engine, these options only have an effect if specified at the document- or project-level metadata.

From Figure options

Expression Printing

デフォルトではチャンク内の最後の式(expression)しか結果として表示されない。

```{python}
"first"
"last"
```
'last'

この挙動は、IPython.InteractiveShell.ast_node_interactivity.default_value'last_expr'となっているためである。(Jupyter shell interactivity)

この挙動を変えて全てのトップレベルの式の評価を出力するには、 以下の様にYAMLヘッダーのipynb-shell-interactivityallに設定するか、

---
title: All expressions
format: html
ipynb-shell-interactivity: all
---

InteractiveShell.ast_node_interactivityを書き換える。

from IPython import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'
```{python}
"first"
"last"
```
'first'
'last'

コードチャンクごとにこの設定を変えるやり方は残念ながら分からなかった。

Sessioninfo

import session_info
session_info.show()
Click to view session information
-----
session_info        1.0.0
-----
Click to view modules imported as dependencies
PIL                         10.4.0
anyio                       NA
appnope                     0.1.4
arrow                       1.3.0
asttokens                   NA
attr                        23.2.0
attrs                       23.2.0
babel                       2.15.0
certifi                     2024.07.04
cffi                        1.16.0
charset_normalizer          3.3.2
comm                        0.2.2
cycler                      0.12.1
cython_runtime              NA
dateutil                    2.9.0.post0
debugpy                     1.8.2
decorator                   5.1.1
defusedxml                  0.7.1
executing                   2.0.1
fastjsonschema              NA
fqdn                        NA
idna                        3.7
ipykernel                   6.29.5
isoduration                 NA
jedi                        0.19.1
jinja2                      3.1.4
json5                       0.9.25
jsonpointer                 3.0.0
jsonschema                  4.23.0
jsonschema_specifications   NA
jupyter_events              0.10.0
jupyter_server              2.14.2
jupyterlab_server           2.27.3
kiwisolver                  1.4.5
markupsafe                  2.1.5
matplotlib                  3.9.1
matplotlib_inline           0.1.7
mpl_toolkits                NA
nbformat                    5.10.4
numpy                       2.0.1
overrides                   NA
packaging                   24.1
pandas                      2.2.3
parso                       0.8.4
platformdirs                4.2.2
prometheus_client           NA
prompt_toolkit              3.0.47
psutil                      6.0.0
pure_eval                   0.2.3
pyarrow                     18.0.0
pydev_ipython               NA
pydevconsole                NA
pydevd                      2.9.5
pydevd_file_utils           NA
pydevd_plugins              NA
pydevd_tracing              NA
pygments                    2.18.0
pyparsing                   3.1.2
pythonjsonlogger            NA
pytz                        2024.2
referencing                 NA
requests                    2.32.3
rfc3339_validator           0.1.4
rfc3986_validator           0.1.1
rpds                        NA
send2trash                  NA
six                         1.16.0
sniffio                     1.3.1
stack_data                  0.6.3
tornado                     6.4.1
traitlets                   5.14.3
uri_template                NA
urllib3                     2.2.2
wcwidth                     0.2.13
webcolors                   24.6.0
websocket                   1.8.0
yaml                        6.0.1
zmq                         26.0.3
-----
IPython             8.26.0
jupyter_client      8.6.2
jupyter_core        5.7.2
jupyterlab          4.2.4
notebook            7.2.1
-----
Python 3.12.2 (main, Feb 25 2024, 03:55:42) [Clang 17.0.6 ]
macOS-13.1-arm64-arm-64bit
-----
Session information updated at 2024-12-07 17:00