backtesting.py
backtesting.py copied to clipboard
The graph is not shown in Jupyter notebook on Docker (VSCode remote containers)
Expected Behavior
The graph should be shown.
Additional info
backtesting.py version is 0.3.3
Python version is 3.10
https://github.com/kernc/backtesting.py/blob/65f54f6819cac5f36fd94ebf0377644c62b4ee3d/backtesting/_plotting.py#L47-L55
In my jupyter notebook on Docker (VSCode remote containers), IS_JUPYTER_NOTEBOOK = 'JPY_PARENT_PID' in os.environ
is False so output_notebook()
is not called.
my os.environ:
environ{'HOSTNAME': 'ae212a2a2420',
'PYTHON_PIP_VERSION': '21.2.4',
'HOME': '/root',
'GPG_KEY': 'A035C8C19219BA821ECEA86B64E628F8D684696D',
'PYTHON_GET_PIP_URL': 'https://github.com/pypa/get-pip/raw/3cb8888cc2869620f57d5d2da64da38f516078c7/public/get-pip.py',
'PATH': '/workspaces/cross/.venv/bin:/workspaces/cross/.venv/bin:/vscode/vscode-server/bin/linux-x64/92d25e35d9bf1a6b16f7d0758f25d48ace11e5b9/bin/remote-cli:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/.local/bin/',
'VSCODE_AGENT_FOLDER': '/root/.vscode-server',
'LANG': 'C.UTF-8',
'SHELL': '/bin/bash',
'PYTHON_VERSION': '3.10.0',
'PYTHON_SETUPTOOLS_VERSION': '57.5.0',
'PWD': '/vscode/vscode-server/bin/linux-x64/92d25e35d9bf1a6b16f7d0758f25d48ace11e5b9',
'PYTHON_GET_PIP_SHA256': 'c518250e91a70d7b20cceb15272209a4ded2a0c263ae5776f129e0d9b5674309',
'VSCODE_HANDLES_SIGPIPE': 'true',
'VSCODE_AMD_ENTRYPOINT': 'vs/workbench/api/node/extensionHostProcess',
'VSCODE_HANDLES_UNCAUGHT_ERRORS': 'true',
'VSCODE_NLS_CONFIG': '{"locale":"en","availableLanguages":{}}',
'REMOTE_CONTAINERS_IPC': '/tmp/vscode-remote-containers-ipc-b8c066b75e8f8d825b20fe6d5f9b7a97a270f221.sock',
'STARSHIP_SHELL': 'bash',
'STARSHIP_SESSION_KEY': '9270280433078331',
'REMOTE_CONTAINERS_SOCKETS': '["/tmp/vscode-ssh-auth-b8c066b75e8f8d825b20fe6d5f9b7a97a270f221.sock"]',
'SHLVL': '1',
'_': '/workspaces/cross/.venv/bin/python',
'SSH_AUTH_SOCK': '/tmp/vscode-ssh-auth-b8c066b75e8f8d825b20fe6d5f9b7a97a270f221.sock',
'REMOTE_CONTAINERS': 'true',
'BROWSER': '/vscode/vscode-server/bin/linux-x64/92d25e35d9bf1a6b16f7d0758f25d48ace11e5b9/bin/helpers/browser.sh',
'VSCODE_CWD': '/vscode/vscode-server/bin/linux-x64/92d25e35d9bf1a6b16f7d0758f25d48ace11e5b9',
'ELECTRON_RUN_AS_NODE': '1',
'VSCODE_IPC_HOOK_CLI': '/tmp/vscode-ipc-3e25cb45-d094-4a75-beef-465805b8543d.sock',
'APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL': '1',
'PYTHONUNBUFFERED': '1',
'VIRTUAL_ENV': '/workspaces/cross/.venv',
'PYTHONIOENCODING': 'utf-8',
'PS1': '(.venv) ',
'PYDEVD_USE_FRAME_EVAL': 'NO',
'TERM': 'xterm-color',
'CLICOLOR': '1',
'PAGER': 'cat',
'GIT_PAGER': 'cat',
'MPLBACKEND': 'module://matplotlib_inline.backend_inline'}
JPY_PARENT_PID is missing.
if I hard code IS_JUPYTER_NOTEBOOK = True
instead of IS_JUPYTER_NOTEBOOK = "JPY_PARENT_PID" in os.environ
in _plotting.py, the graph is successfully shown.
I guess maybe we could adapt the condition to:
IS_JUPYTER_NOTEBOOK = ('JPY_PARENT_PID' in os.environ or
'inline' in os.environ.get('MPLBACKEND', ''))
if that's reliable?
Could you investigate it or want to PR it?
Otherwise, the hack you've found is perfectly valid, including the preferred:
import backtesting
backtesting.set_bokeh_output(notebook=True)
Thank you for the reply.
This worked pretty good !
IS_JUPYTER_NOTEBOOK = ('JPY_PARENT_PID' in os.environ or
'inline' in os.environ.get('MPLBACKEND', ''))
Now my graph is shown without the following snippet. What's this for ?
import backtesting
backtesting.set_bokeh_output(notebook=True)
This worked pretty good !
Great, so we can change it in the source and hopefully fix similar issues for everyone. Thanks!
What's this for ?
import backtesting backtesting.set_bokeh_output(notebook=True)
This is how you officially force notebook/non-notebook output. Hardcoding values into third-party projects is not a particularly robust approach.
Since backtesting.Backtest.plot(open_browser=True)
by default, set open_browser=False
after backtesting.set_bokeh_output(notebook=True)
works for me in vscode ipynb.