Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plotting from ipython #769

Closed
kakila opened this issue Dec 20, 2022 · 8 comments
Closed

plotting from ipython #769

kakila opened this issue Dec 20, 2022 · 8 comments

Comments

@kakila
Copy link

kakila commented Dec 20, 2022

Hi,
Thanks for the nice tool.
I have tried to search the documentation for the way to plot form within ipython.
Is this possible? I tried several backends but none is creating a window.

Thanks

@marcomusy
Copy link
Owner

Hi, sorry for the late reply, have you tried:

settings.default_backend = "vtk" 

@paulbrodersen
Copy link
Contributor

paulbrodersen commented Apr 18, 2024

Hi, great library but I just ran into the same issue. While setting the default backend to VTK solves the problem, is there an explanation why vedo defaults to "2d" in an ipython REPL?

vedo --info
# vedo version      : 2024.5.1  (https://vedo.embl.es)             
# vtk version       : 9.3.0
# numpy version     : 1.23.5
# python version    : 3.10.14 | packaged by conda-forge | (main, Mar 20 2024, 12:45:18) [GCC 12.3.0]
# python interpreter: /home/paul/src/miniconda3/envs/brainrender_dev/bin/python3.10
# installation point: /home/paul/src/miniconda3/envs/brainrender_dev/lib/python3.10/site-packages/v
# system            : Linux 5.4.0-148-generic posix x86_64
# k3d version       : 2.16.1

When running from shell:

python -c "import vedo; print(vedo.settings.default_backend)"
# vtk

When running in an IPython REPL:

(brainrender_dev) paul@paul-XPS-15-9560:~/src/BrainRender$ ipython
Python 3.10.14 | packaged by conda-forge | (main, Mar 20 2024, 12:45:18) [GCC 12.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.23.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import vedo; print(vedo.settings.default_backend)
2d

Presumably there are good reasons for the switch, but maybe it's worth raising a warning whenever the '2d' backend is selected automatically? It took a hot minute to narrow the issue down to a ipython-vedo interaction.

@marcomusy
Copy link
Owner

Hi - yes indeed there is a "good" reason ... the visualization part is normally delegated to an opengl graphic rendering which pops up in a separate window.
In ipyhton envs this is not what normally one would expect hence the change in the behavior.
I will add this explanation in the docs to clarify it, thanks for drawing my attention to it!

@paulbrodersen
Copy link
Contributor

Hi, I think there is some confusion here with Jupyter notebooks (which used to be called IPython notebooks).
I am talking about the IPython REPL, a replacement for the standard python console. The ipython REPL is a terminal program, so figures (matplotlib, cairo, etc) are spawned as their own processes in their own windows.

@marcomusy
Copy link
Owner

Oh OK sorry!
We do this in vedo/settings.py in Settings.__init__():

        self.default_backend = "vtk"
        try:
            get_ipython()
            self.default_backend = "2d"
        except NameError:
            pass

is there a way to distinguish or separate the case of a REPL vs jupyter env?

@paulbrodersen
Copy link
Contributor

A couple of solutions have been discussed on Stackoverflow.

The first answer looks reasonably robust and works on my machine:

def is_notebook() -> bool:
    try:
        shell = get_ipython().__class__.__name__
        if shell == 'ZMQInteractiveShell':
            return True   # Jupyter notebook or qtconsole
        elif shell == 'TerminalInteractiveShell':
            return False  # Terminal running IPython
        else:
            return False  # Other type (?)
    except NameError:
        return False      # Probably standard Python interpreter

So the snippet above would be:

if is_notebook():
    self.default_backend = "2d"
else:
    self.default_backend = "vtk"

@paulbrodersen
Copy link
Contributor

paulbrodersen commented Apr 19, 2024

Also note the comment below that answer on detecting google colab notebooks:

get_ipython().__class__.__module__ == "google.colab._shell"

Though is suspect that vedo is probably incompatible with colab anyway due to the lack of display parameters?

@marcomusy
Copy link
Owner

Thanks a lot Paul! This is very useful!

suspect that vedo is probably incompatible with colab anyway due to the lack of display parameters

in fact you can run vedo in a colab env using this.

marcomusy added a commit that referenced this issue Apr 30, 2024
Mitigate issue #769: don't set backend to '2d' in IPython REPLs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants