plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

Can't render in jupyter lab with history_manager disabled

Open jabbera opened this issue 3 years ago • 2 comments

Running the following code with history_manager diabled in jupyerlab

import plotly.express as px
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])

import plotly.io as pio
pio.renderers.default = 'iframe'
fig.show()

yields the following error:

File ~/venvs/plotly/lib/python3.10/site-packages/plotly/io/_base_renderers.py:612, in IFrameRenderer.build_filename(self)
    610 def build_filename(self):
    611     ip = IPython.get_ipython() if IPython else None
--> 612     cell_number = list(ip.history_manager.get_tail(1))[0][1] + 1 if ip else 0
    613     filename = "{dirname}/figure_{cell_number}.html".format(
    614         dirname=self.html_directory, cell_number=cell_number
    615     )
    616     return filename

IndexError: list index out of range

Script to reproduce:

Run this, open the demo.ipynb try to execute the cell.

python3.10 -m venv .venv plotly
source plotly/bin/activate
pip install "jupyterlab>=3" "ipywidgets>=7.6" pandas 
pip install plotly
mkdir -p ipyc/profile_default

echo "c = get_config()
c.HistoryManager.enabled = False" > ipyc/profile_default/ipython_kernel_config.py

cat > ./demo.ipynb <<- EOM
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47466be7-d06a-454f-ba8e-8042889d7b6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import plotly.express as px\n",
    "fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])\n",
    "\n",
    "import plotly.io as pio\n",
    "pio.renderers.default = 'iframe'\n",
    "fig.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
EOM

IPYTHONDIR=./ipyc jupyter lab

jabbera avatar Sep 07 '22 20:09 jabbera

Thanks @jabbera, looks like a fairly simple fix - fall back on get_ipython().execution_count or just our own counter like we use in CoCalcRenderer.

Curious though, what's the motivation to disable history_manager?

alexcjohnson avatar Sep 07 '22 20:09 alexcjohnson

@alexcjohnson We are running in jupyterhub and the home folder is an NFS share which doesn't play well with sqlite.

jabbera avatar Sep 07 '22 21:09 jabbera

Hi - we are tidying up stale issues and PRs in Plotly's public repositories so that we can focus on things that are still important to our community. Since this one has been sitting for a while, I'm going to close it; if it is still a concern, please add a comment letting us know what recent version of our software you've checked it with so that I can reopen it and add it to our backlog. If you'd like to submit a PR, we'd be happy to prioritize a review, and if it's a request for tech support, please post in our community forum. Thank you - @gvwilson

gvwilson avatar Jul 11 '24 14:07 gvwilson