K3D-tools / K3D-jupyter

K3D lets you create 3D plots backed by WebGL with high-level API (surfaces, isosurfaces, voxels, mesh, cloud points, vtk objects, volume renderer, colormaps, etc). The primary aim of K3D-jupyter is to be easy for use as stand alone package like matplotlib, but also to allow interoperation with existing libraries as VTK.
MIT License
917 stars 123 forks source link

Does K3D allow plotting multiple texts as single object? #391

Closed zftan0709 closed 1 year ago

zftan0709 commented 1 year ago

Description

I am trying to plot multiple text box as single object (e.g. 3000 text box) and would like to have the option of hiding all of them in the snapshot. Is there any thing like lines object, but for texts so I can have multiple text box in a single object?

What I Did

Currently, I am adding all 3000 text box one by one and it actually leads to low fps when viewing the snapshot.

artur-trzesiok commented 1 year ago

Hi!

Yes, K3D based on three.js and three.js is not created to handle 3000 objects on scene. Overhead with opbject handling is too big. Still that amount of text objects can be hard for browser.

We don't have support for that (yet). Give me a chance to do some performance tests before I will respond, ok?

artur-trzesiok commented 1 year ago

For sure I highly recommend to disable katex interpretation if you don't use it by seting is_html=True

k3d.text("lorem ipsum", coord,is_html=True)

What performance do you have then?

artur-trzesiok commented 1 year ago

@zftan0709 in next release I will add that:

# Multipile texts in single object

import numpy as np

N = 200
positions = np.random.randn(N,3).astype(np.float32)

plot2 = k3d.plot()
plot2 += k3d.text(["lorem ipsum"] * N, positions, is_html=True)
plot2.display()

Plan is to have it for k3d.text, k3d.text2d, k3d.texture_text and k3d.label

artur-trzesiok commented 1 year ago

Hi! Good news!

Please look at last cells here:

Section "# Multipile texts in single object"

artur-trzesiok commented 1 year ago

@zftan0709 Please check k3d@2.15.1 and tell me if that corespond with your needs

zftan0709 commented 1 year ago

Hey @artur-trzesiok, thanks for addressing this issue and working on this, really appreciate it! I did some quick test on the latest k3d@2.15.1, but seems like I'm getting some k3d loader failure.

Here's what I did: Installation: pip install k3d=2.15.1

Run:

import numpy as np
import k3d

N = 20
positions = np.random.randn(N, 3).astype(np.float32)
plot2 = k3d.plot()
obj = k3d.label(["lorem ipsum"] * N, positions, is_html=True, mode='dynamic')
plot2 += obj

with open("/home/zftan/testk3d.html", 'w') as f:
    f.write(plot2.get_snapshot())
f.close()

Not sure if this is related, I also noticed that the Js version in the k3d snapshot panel is 2.15.0 version. I have also attached the generated snapshot file. Am I missing something here? testk3d.html.zip

artur-trzesiok commented 1 year ago

Hi @zftan0709

Thats is wrong and that is a reason why you have wrong results. Many things can happen but most probably is a browser cache. Please do after pip install k3d=2.15.1 a restart of jupyter and refresh a browser and make sure that browser do not use cache

After k3d.plot() you should see: image

zftan0709 commented 1 year ago

Hi @artur-trzesiok , thanks again for the prompt reply. I have tried the suggested steps, including reinstalling everything on another new computer, but still getting the same results. From what I see, pip install k3d=2.151 shows that k3d has 2.15.1 version

Requirement already up-to-date: k3d in /home/zftan/.local/lib/python3.8/site-packages (2.15.1)
Requirement already satisfied, skipping upgrade: traitlets in /home/zftan/.local/lib/python3.8/site-packages (from k3d) (5.4.0)
Requirement already satisfied, skipping upgrade: msgpack in /home/zftan/.local/lib/python3.8/site-packages (from k3d) (0.6.2)
Requirement already satisfied, skipping upgrade: ipywidgets<9.0.0,>=7.0.0 in /home/zftan/.local/lib/python3.8/site-packages (from k3d) (8.0.2)
Requirement already satisfied, skipping upgrade: numpy in /home/zftan/.local/lib/python3.8/site-packages (from k3d) (1.23.3)
Requirement already satisfied, skipping upgrade: traittypes in /home/zftan/.local/lib/python3.8/site-packages (from k3d) (0.2.1)
Requirement already satisfied, skipping upgrade: widgetsnbextension~=4.0 in /home/zftan/.local/lib/python3.8/site-packages (from ipywidgets<9.0.0,>=7.0.0->k3d) (4.0.3)
Requirement already satisfied, skipping upgrade: ipykernel>=4.5.1 in /home/zftan/.local/lib/python3.8/site-packages (from ipywidgets<9.0.0,>=7.0.0->k3d) (6.15.3)
Requirement already satisfied, skipping upgrade: jupyterlab-widgets~=3.0 in /home/zftan/.local/lib/python3.8/site-packages (from ipywidgets<9.0.0,>=7.0.0->k3d) (3.0.3)
Requirement already satisfied, skipping upgrade: ipython>=6.1.0 in /home/zftan/.local/lib/python3.8/site-packages (from ipywidgets<9.0.0,>=7.0.0->k3d) (8.5.0)
Requirement already satisfied, skipping upgrade: matplotlib-inline>=0.1 in /home/zftan/.local/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets<9.0.0,>=7.0.0->k3d) (0.1.6)
Requirement already satisfied, skipping upgrade: nest-asyncio in /home/zftan/.local/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets<9.0.0,>=7.0.0->k3d) (1.5.5)
Requirement already satisfied, skipping upgrade: packaging in /home/zftan/.local/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets<9.0.0,>=7.0.0->k3d) (21.3)
Requirement already satisfied, skipping upgrade: psutil in /home/zftan/.local/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets<9.0.0,>=7.0.0->k3d) (5.9.2)
Requirement already satisfied, skipping upgrade: jupyter-client>=6.1.12 in /home/zftan/.local/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets<9.0.0,>=7.0.0->k3d) (7.3.5)
Requirement already satisfied, skipping upgrade: pyzmq>=17 in /home/zftan/.local/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets<9.0.0,>=7.0.0->k3d) (24.0.0)
Requirement already satisfied, skipping upgrade: tornado>=6.1 in /home/zftan/.local/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets<9.0.0,>=7.0.0->k3d) (6.2)
Requirement already satisfied, skipping upgrade: debugpy>=1.0 in /home/zftan/.local/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets<9.0.0,>=7.0.0->k3d) (1.6.3)
Requirement already satisfied, skipping upgrade: stack-data in /home/zftan/.local/lib/python3.8/site-packages (from ipython>=6.1.0->ipywidgets<9.0.0,>=7.0.0->k3d) (0.5.0)
Requirement already satisfied, skipping upgrade: pygments>=2.4.0 in /home/zftan/.local/lib/python3.8/site-packages (from ipython>=6.1.0->ipywidgets<9.0.0,>=7.0.0->k3d) (2.13.0)
Requirement already satisfied, skipping upgrade: decorator in /home/zftan/.local/lib/python3.8/site-packages (from ipython>=6.1.0->ipywidgets<9.0.0,>=7.0.0->k3d) (5.1.1)
Requirement already satisfied, skipping upgrade: backcall in /home/zftan/.local/lib/python3.8/site-packages (from ipython>=6.1.0->ipywidgets<9.0.0,>=7.0.0->k3d) (0.2.0)
Requirement already satisfied, skipping upgrade: prompt-toolkit<3.1.0,>3.0.1 in /home/zftan/.local/lib/python3.8/site-packages (from ipython>=6.1.0->ipywidgets<9.0.0,>=7.0.0->k3d) (3.0.31)
Requirement already satisfied, skipping upgrade: jedi>=0.16 in /home/zftan/.local/lib/python3.8/site-packages (from ipython>=6.1.0->ipywidgets<9.0.0,>=7.0.0->k3d) (0.18.1)
Requirement already satisfied, skipping upgrade: pexpect>4.3; sys_platform != "win32" in /usr/lib/python3/dist-packages (from ipython>=6.1.0->ipywidgets<9.0.0,>=7.0.0->k3d) (4.6.0)
Requirement already satisfied, skipping upgrade: pickleshare in /home/zftan/.local/lib/python3.8/site-packages (from ipython>=6.1.0->ipywidgets<9.0.0,>=7.0.0->k3d) (0.7.5)
Requirement already satisfied, skipping upgrade: pyparsing!=3.0.5,>=2.0.2 in /home/zftan/.local/lib/python3.8/site-packages (from packaging->ipykernel>=4.5.1->ipywidgets<9.0.0,>=7.0.0->k3d) (3.0.9)
Requirement already satisfied, skipping upgrade: python-dateutil>=2.8.2 in /home/zftan/.local/lib/python3.8/site-packages (from jupyter-client>=6.1.12->ipykernel>=4.5.1->ipywidgets<9.0.0,>=7.0.0->k3d) (2.8.2)
Requirement already satisfied, skipping upgrade: jupyter-core>=4.9.2 in /home/zftan/.local/lib/python3.8/site-packages (from jupyter-client>=6.1.12->ipykernel>=4.5.1->ipywidgets<9.0.0,>=7.0.0->k3d) (4.11.1)
Requirement already satisfied, skipping upgrade: entrypoints in /usr/lib/python3/dist-packages (from jupyter-client>=6.1.12->ipykernel>=4.5.1->ipywidgets<9.0.0,>=7.0.0->k3d) (0.3)
Requirement already satisfied, skipping upgrade: asttokens in /home/zftan/.local/lib/python3.8/site-packages (from stack-data->ipython>=6.1.0->ipywidgets<9.0.0,>=7.0.0->k3d) (2.0.8)
Requirement already satisfied, skipping upgrade: executing in /home/zftan/.local/lib/python3.8/site-packages (from stack-data->ipython>=6.1.0->ipywidgets<9.0.0,>=7.0.0->k3d) (1.0.0)
Requirement already satisfied, skipping upgrade: pure-eval in /home/zftan/.local/lib/python3.8/site-packages (from stack-data->ipython>=6.1.0->ipywidgets<9.0.0,>=7.0.0->k3d) (0.2.2)
Requirement already satisfied, skipping upgrade: wcwidth in /home/zftan/.local/lib/python3.8/site-packages (from prompt-toolkit<3.1.0,>3.0.1->ipython>=6.1.0->ipywidgets<9.0.0,>=7.0.0->k3d) (0.2.5)
Requirement already satisfied, skipping upgrade: parso<0.9.0,>=0.8.0 in /home/zftan/.local/lib/python3.8/site-packages (from jedi>=0.16->ipython>=6.1.0->ipywidgets<9.0.0,>=7.0.0->k3d) (0.8.3)
Requirement already satisfied, skipping upgrade: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil>=2.8.2->jupyter-client>=6.1.12->ipykernel>=4.5.1->ipywidgets<9.0.0,>=7.0.0->k3d) (1.14.0)

However, when I tried printing the k3d version in my script, it shows that it's version 2.15.0

import k3d
print(k3d.__version__)

2.15.0

Seems like pip install does not actually install the latest version

artur-trzesiok commented 1 year ago

Hm. Can you share output from "print(k3d.HERE)"?

goal is that k3d.__version__ and numbers in screenshots that I shared should be exactly the same.

zftan0709 commented 1 year ago

Of course, here's the output python3

Python 3.8.16 (default, Dec  8 2022, 03:18:29) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import k3d
>>> print(k3d.__version__)
2.15.0
>>> print(k3d.HERE)
/usr/local/lib/python3.8/site-packages/k3d
>>> exit()

pip show k3d

Name: k3d
Version: 2.15.1
Summary: 
Home-page: 
Author: 
Author-email: 
License: The MIT License

        Copyright © 2010-2019 K3D authors

        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:

        The above copyright notice and this permission notice shall be included in
        all copies or substantial portions of the Software.

        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
        THE SOFTWARE.
Location: /usr/local/lib/python3.8/site-packages
Requires: ipywidgets, msgpack, numpy, traitlets, traittypes
Required-by: 

I also tried it on newly created Google Colab

pip install k3d==2.15.1
import k3d
print(k3d.__version__)

and the result is still 2.15.0

artur-trzesiok commented 1 year ago

Thanks! So i feel that we found a problem

Requirement already up-to-date: k3d in /home/zftan/.local/lib/python3.8/site-packages (2.15.1)

So pip install k3d=2.15.1 try to install that in your user space (/home/zftan/.local) but k3d that you actually using is

print(k3d.HERE) /usr/local/lib/python3.8/site-packages/k3d

So system level library. Please double check that because its look like in jupyter python and "console python" you have installed different k3d packages

btw I notice too that I made mistake during a release with version. I will publish 2.15.2 soon

artur-trzesiok commented 1 year ago

@zftan0709 can you try pip install k3d==2.15.2 ?

zftan0709 commented 1 year ago

It's working now and I'm able to plot all the texts in single label! Thanks again for working on this, @artur-trzesiok, really appreciate the effort!