Python plotly only works when executed in console
System details:
Windows 11 Ubuntu 22
Positron and OS details:
Interpreter details:
Python 3.12.2
Describe the issue:
Running the following code using the play button "Run Python File in Console" results in an error, but running line by line in the console works.
Steps to reproduce the issue:
import plotly.express as px
fig = px.bar(x=["a", "b", "c"], y=[1, 3, 2])
fig
Expected or desired behavior:
Figure to show when clicking the run button as well as line by line in the console.
Were there any error messages in the UI, Output panel, or Developer Tools console?
I think part of this is just how Python works. Since the script is named plotly, when you do import plotly.express it's actually importing the script and failing because it's not a package.
You can fix that issue by renaming the module to something other than plotly.py.
After that fix, it still doesn't display anything in the plots pane for me though.
@seeM , yes I'm going to go through our example codebase and make sure to fix this and any other with similar filenames.
I think we mistakenly assigned this to you, @jonvanausdeln. As @seeM mentioned, even after the rename there is an issue.
I think we mistakenly assigned this to you, @jonvanausdeln. As @seeM mentioned, even after the rename there is an issue.
I think you are correct. I will move it back to backlog and remove my name. I will still fix the filenames (and tests that use them). But I just checked and yes, it still won't run with the filename changed.
Also note that plotly doesn't work in a notebook:
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"library(plotly)\n",
"# install.packages('mvtnorm')\n",
"\n",
"# change this to scale up or down per cell\n",
"# 500,000 will create about a 77 Mb notebook on disk\n",
"data_size = 500000\n",
"\n",
"s <- matrix(c(1, -.75, -.75, 1), ncol = 2)\n",
"obs <- mvtnorm::rmvnorm(data_size, sigma = s)\n",
"\n",
"fig <- plot_ly(x = obs[,1], y = obs[,2])\n",
"fig2 <- subplot(\n",
" fig %>% add_markers(alpha = 0.2),\n",
" fig %>% add_histogram2d()\n",
")\n",
"\n",
"fig2"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3",
"path": "/usr/local/share/jupyter/kernels/python3"
},
"language_info": {
"name": "r"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
An altair plot also does not show in the plots pane when run from a file. E.g. code found here
But does show if executed line-by-line using ctrl+enter
Marking as duplicate of https://github.com/posit-dev/positron/issues/7782