0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

#.ipynbファイルを.pyに変換 (Docker,JupyterLab)

Posted at

Dockerコンテナ内で動かしている.ipynbファイルを.pyファイルに変換する方法を備忘録として記しておきます。

1つのコードで簡単に行えます。

変換したい.ipynbファイルの最後に以下のコードを書いて実行 (Shift + Enter) するだけで完了です。

*.ipynb
import subprocess
subprocess.run(['jupyter', 'nbconvert', '--to', 'python', '[ipynbファイルがあるディレクトリパスを入力]/*.ipynb'])

'python'という箇所がファイル拡張子の定義場所となっています。
('Script'に変更すると、.txt形式になります)

上のコードを実行することにより、変換された.pyファイルが同一ディレクトリ上に新規作成されます。
同一ディレクトリ上にある全ての.ipynbファイルが変換されるため、特定の1つのみを.pyファイルに変換したい場合は、ファイル名を指定することにより可能となります。
(*.ipynbの箇所を存在するファイル名に変更)
出力されない場合は、パスに誤りがある可能性があるため、ログなどを再度確認してみてください。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?