voila-dashboards / voila

Voilà turns Jupyter notebooks into standalone web applications
https://voila.readthedocs.io
Other
5.32k stars 497 forks source link

Embed in pyqt5 application #399

Open luiztauffer opened 4 years ago

luiztauffer commented 4 years ago

Hi, I’m building a Pyqt5 GUI and I’m interested in embedding the voila-rendered notebooks in it. Is there any straightforward way of doing this? Or else, where would you suggest me to start looking at? Thanks for the work in this great project!

luiztauffer commented 4 years ago

I did some progress with it, I can now display Voila form my pyqt app using QwebEngineView. To completely solve my problem, I still need to call voila programmatically, from within my python code: 1 – with the --no-browser and --port XXXX options 2 – close the voila process

any suggestions on how to do that the best way with a python code?

SylvainCorlay commented 4 years ago

Hey @luiztauffer. This is an idea that @astrofrog experimented with at a workshop in may.

I don't know if his code is available somewhere but I presume it could be a good start.

Ping @astrofrog.

luiztauffer commented 4 years ago

Hi @SylvainCorlay thanks for the reply!

I managed to make it work somehow, although in a quite inefficient way I believe. I’m starting a Thread for voila separate from my main Pyqt Thread and I’m calling it using: os.system("voila "+self.filename+'.ipynb --no-browser --port '+str(self.port))

I wonder if you could point me to a couple of things:

  1. How to run voila form a python script without using os.system? I’ve been trying with from voila.app import Voila but no success so far.
  2. How to send a stop/kill/terminate signal to an ongoing voila process? I’m terminating my Thread but that doesn’t seem to stop the voila process.
  3. How to export the current state to a json file? My main use will be to let users edit the layout of a complex Plotly figure (with ipywidgets) and I would like to recover exactly the user modifications.

Also good to hear that @astrofrog worked on something similar. Did you make progress on that? Would you mind sharing your opinion? =)

jtpio commented 4 years ago

cc @cmaureir who might be interested in this too.

cmaureir commented 4 years ago

@jtpio found the repo by @astrofrog https://github.com/astrofrog/voila-qt-app the idea is similar to what you did @luiztauffer, which is just starting the voila app on a thread and then just display that on a QWebEngine page. To answer your questions I will need to dig a little bit more into the voila code, but in any case, check the repo out!

cmaureir commented 4 years ago

After looking at this a little bit more deeper, I think there are three ways to proceed:

  1. We continue the work by @astrofrog to provide a standalone application based on a certain notebook, adding maybe a couple of options to refresh the notebook, or to load another one.
  2. We extend the previous approach with a workaround to have Qt components modifying the current notebook. For this, we would need to keep a Python equivalent of the notebook so we can parse the file and access a couple of variables so we offer a way to modify stuff from external components like QSpinBox, QComboBox, etc. After we modify the code, we would need to convert the code back to a notebook and refresh the web view that is displaying the notebook.
  3. We jump to WebAssembly by writing C++ components that have equivalents for the ipywidgets so we can render an app completely in wasm, with Qt components and some new ones from plotly or other modules. This, of course, requires us to interact with plotly from C++, which I don't know if it's possible.

Any further ideas?

maartenbreddels commented 4 years ago

FYI, the output of @astrofrog 's binaries can be easily reused, using pip install --target ... you can install extra packages in the binaries, and the notebooks can also be replaced (for instance a static html page with links to the notebooks).

luiztauffer commented 4 years ago

Hi @SylvainCorlay @jtpio @maartenbreddels @cmaureir, for your update, I ended up creating a similar thing that proposed by @astrofrog, a Qt widget that embeds the Voila output and can be easily included in any PySide2 project:

https://github.com/luiztauffer/qtvoila

luiztauffer commented 4 years ago

It's still not optimal, since it for now the widget:

  1. gets a code from the user
  2. saves this code in a one-cell Jupyter Notebook file
  3. sends the file to Voila

It would be great to have a way around it and passing the nbformat object directly to Voila!