Skip to content

Commit

Permalink
Merge pull request #5 from cover-me/draft_improve_static
Browse files Browse the repository at this point in the history
Draft improve static
  • Loading branch information
cover-me committed Apr 22, 2020
2 parents ab06ffb + 6959509 commit c90a7f5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
14 changes: 7 additions & 7 deletions README.md
Expand Up @@ -18,11 +18,9 @@ qtplotter.py: The main code is here.

## Preview .ipynb files

These files can be directly previewed on GitHub. However, GitHub doesn't render ipywidgets (which is used for interaction). You can preview notebooks on [jupyter.org](https://nbviewer.jupyter.org/) for a better experience. (Links: [index.ipynb](https://nbviewer.jupyter.org/github/cover-me/qtplotter/blob/master/index.ipynb), [example.ipynb](https://nbviewer.jupyter.org/github/cover-me/qtplotter/blob/master/example.ipynb))
These files can be directly previewed on GitHub. GitHub doesn't render ipywidgets (which is used for interaction). [nbviewer.jupyter.org](https://nbviewer.jupyter.org/) may provide better previews. (Links: [index.ipynb](https://nbviewer.jupyter.org/github/cover-me/qtplotter/blob/master/index.ipynb), [example.ipynb](https://nbviewer.jupyter.org/github/cover-me/qtplotter/blob/master/example.ipynb)). However, even Jupyter nbviewer can not render complicated widgets correctly at this moment. Many widgets just disappear in all notebook viewers (a quick check: export notebook as an HTML file and check if anything is missing). As notebooks are used for sharing and viewing, it's important to avoid displaying images in a widget if this problem has not been solved. And here is how:

However, even Jupyter nbviewer can not render complicated widgets correctly at this moment. Many widgets just disappear in all notebook viewers (a quick check: export notebook as an HTML file and check if there is anything missing). As notebooks are used for sharing and viewing, it's important to avoid images in widgets if this problem has not been solved. And here is how:

Run `Player.PLAYER_STATIC = True` once and all "interactive" figures generated by `play()` will be just static snapshots that can be directly saved in a notebook for sharing/viewing. Remember to delete `Player.PLAYER_STATIC = True` before saving/sharing the notebook.
Run `Player.PLAYER_STATIC = True` once and all "interactive" figures generated by `play()` will be just static snapshots that can be directly saved in a notebook for sharing/viewing. Remember to comment out or delete `Player.PLAYER_STATIC = True` before saving/sharing the notebook.

## Run .ipynb files in the cloud

Expand All @@ -32,13 +30,15 @@ Click the badges at the begging of this readme file to launch index.ipynb.

The notebooks can be run on [mybinder.org](https://mybinder.org/) directly so that the shared data, no matter local or online, can be visualized interactively with a few clicks, by anyone, anywhere.

It usually takes less than 20 seconds to launch, but sometimes more than 1 minute. After having launched, use the `open->files...` menu to go to the files page where you can open other notebooks or download generated files.
It usually takes about 40 seconds to launch, sometimes less than 20 s, sometimes more than 1 minute. After having launched, use the `open->files...` menu to go to the files page where you can open other notebooks or download generated files.

### Colab

It also works on Colab. Colab has native support for table of contents, which is awesome for notebooks. Colab doesn't support ipywidgets very well (no `%matplotlib widget` at all). But `%matplotlib inline` mode works (even for interaction, though slower than `widget` as `inline` backend redraws the whole figure every time).
It also works on Colab. Colab has native support for table of contents, which is awesome for notebooks. Colab open notebooks very fast. No one wants to wait for opening a notebook.

The downside of Colab is that it doesn't support ipywidgets very well. No supoort for `%matplotlib widget` at all, but `%matplotlib inline` mode works (even for interaction, though a bit slower than `widget` as `inline` backend redraws the whole figure every time). Everything in a Tab widget goes to the first Tab page, but that is not a scientific issue.

Colab doesn't import additional files from GitHub repositories automatically as Binder does. One has to upload qtplotter.py to Colab's temporary runtime manually in order to run the notebook, or copy-paste-run code from qtplotter.py in the notebook. The correct "uploading file" menu is on the left of the Colab interface. There is a "folder" icon below the "show table of contents" icon and the "show code snippet pane" icon. Uploading files to google drive also works but requires some hacking. Another way to dump files from this repositories is to run the code below in the notebook:
Colab doesn't import additional files from GitHub repositories automatically as Binder does. One has to upload qtplotter.py to Colab's temporary runtime manually to run the notebook, or copy-paste-run code from qtplotter.py in the notebook. The correct "uploading file" menu is on the left of the Colab interface. There is a "folder" icon below the "show table of contents" icon and the "show code snippet pane" icon. Uploading files to google drive also works but requires some hacking. Another way to dump files from this repositories is to run the code below in the notebook:

```
!git clone https://github.com/cover-me/qtplotter
Expand Down
66 changes: 37 additions & 29 deletions qtplotter.py
Expand Up @@ -408,8 +408,28 @@ def __init__(self,path_or_url,**kw):
self.y = y
self.w = w
self.kw = kw

# UI

self.create_ui()
self.draw(event=None)

if mpl.get_backend() == 'module://ipympl.backend_nbagg':#ipympl backend. Not good at this moment. But faster
self.s_gamma.observe(self.on_gamma_change,'value')
self.s_vlim.observe(self.on_vlim_change,'value')
self.c_cmap.observe(self.on_cmap_change,'value')
self.s_xpos.observe(self.on_xpos_change,'value')
self.s_ypos.observe(self.on_ypos_change,'value')
self.fig.canvas.mpl_connect('button_press_event', self.on_mouse_click)
else:# inline mode
self.s_gamma.observe(self.draw,'value')
self.s_vlim.observe(self.draw,'value')
self.c_cmap.observe(self.draw,'value')
self.s_xpos.observe(self.draw,'value')
self.s_ypos.observe(self.draw,'value')
self.tb_showtools.observe(self.on_showtools_change,'value')
self.b_expMTX.on_click(self.exportMTX)

def create_ui(self):
x,y,w = self.x,self.y,self.w
x0 = x[0]
y0 = y[:,0]
xmin,xmax,dx = x[0,0],x[0,-1],x[0,1]-x[0,0]
Expand All @@ -436,33 +456,21 @@ def __init__(self,path_or_url,**kw):
self.tb_showtools.layout.width='50px'
## Top layer ui
ui = widgets.Box([self.t_tools,self.tb_showtools])
self.out = widgets.Output()

if 'gamma' in kw:
self.s_gamma.value = kw['gamma']
if 'vlim' in kw:
self.s_vlim.value = kw['vlim']
if 'cmap' in kw:
self.c_cmap.value = kw['cmap']

display(ui,self.out)
self.draw(None)
if mpl.get_backend() == 'module://ipympl.backend_nbagg':#ipympl backend. Not good at this moment. But faster
self.s_gamma.observe(self.on_gamma_change,'value')
self.s_vlim.observe(self.on_vlim_change,'value')
self.c_cmap.observe(self.on_cmap_change,'value')
self.s_xpos.observe(self.on_xpos_change,'value')
self.s_ypos.observe(self.on_ypos_change,'value')
self.fig.canvas.mpl_connect('button_press_event', self.on_mouse_click)
else:# inline mode
self.s_gamma.observe(self.draw,'value')
self.s_vlim.observe(self.draw,'value')
self.c_cmap.observe(self.draw,'value')
self.s_xpos.observe(self.draw,'value')
self.s_ypos.observe(self.draw,'value')
self.tb_showtools.observe(self.on_showtools_change,'value')
self.b_expMTX.on_click(self.exportMTX)

self.out = widgets.Output()
if 'gamma' in self.kw:
self.s_gamma.value = self.kw['gamma']
if 'vlim' in self.kw:
self.s_vlim.value = self.kw['vlim']
if 'cmap' in self.kw:
self.c_cmap.value = self.kw['cmap']

if Player.PLAYER_STATIC:
from IPython.core.display import HTML
display(HTML('<button style="border:none;" title="For interaction, run the cell first.">+...</button>'))
else:
display(ui,self.out)


def draw(self,event):
# axs
fig, axs = plt.subplots(1,2,figsize=(6.5,2.5),dpi=100)#main plot and h linecut
Expand Down

0 comments on commit c90a7f5

Please sign in to comment.