positron icon indicating copy to clipboard operation
positron copied to clipboard

Python plotly only works when executed in console

Open jonvanausdeln opened this issue 1 year ago • 4 comments

System details:

Windows 11 Ubuntu 22

Positron and OS details:

2024.10.0-13

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

Image

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?

jonvanausdeln avatar Oct 03 '24 23:10 jonvanausdeln

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 avatar Oct 08 '24 14:10 seeM

@seeM , yes I'm going to go through our example codebase and make sure to fix this and any other with similar filenames.

jonvanausdeln avatar Oct 08 '24 15:10 jonvanausdeln

I think we mistakenly assigned this to you, @jonvanausdeln. As @seeM mentioned, even after the rename there is an issue.

testlabauto avatar Oct 09 '24 17:10 testlabauto

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.

jonvanausdeln avatar Oct 09 '24 17:10 jonvanausdeln

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
}

testlabauto avatar Nov 01 '24 13:11 testlabauto

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

jonvanausdeln avatar Nov 04 '24 19:11 jonvanausdeln

Marking as duplicate of https://github.com/posit-dev/positron/issues/7782

isabelizimm avatar Dec 02 '25 15:12 isabelizimm