Is it possible to open Napari (in full-screen mode) from a script?

Hi,

I am running a python script as follows:
napari example.py
It works fine, it opens the Viewer in the default size, but I would to open the viewer in full-screen size. Is this possible?

According to the help, there is not such option:

napari --help
usage:
napari command line viewer.

positional arguments:
paths path(s) to view.

optional arguments:
-h, --help show this help message and exit
-v, --verbose increase output verbosity
–version show program’s version number and exit
–info show system information and exit
–citation show citation information and exit
–stack concatenate multiple input files into a single stack.
–plugin PLUGIN specify plugin name when opening a file
–layer-type TYPE force file to be interpreted as a specific layer type. one of {‘image’,
‘vectors’, ‘shapes’, ‘labels’, ‘points’, ‘surface’}

optional layer-type-specific arguments (precede with ‘–’):
Image:
attenuation, blending, channel-axis, colormap, contrast-limits, data,
gamma, interpolation, iso-threshold, metadata, multiscale, name,
opacity, rendering, rgb, scale, translate, visible
Labels:
blending, color, data, metadata, multiscale, name, num-colors,
opacity, properties, scale, seed, translate, visible
Points:
blending, data, edge-color, edge-color-cycle, edge-colormap,
edge-contrast-limits, edge-width, face-color, face-color-cycle,
face-colormap, face-contrast-limits, metadata, n-dimensional, name,
opacity, properties, scale, size, symbol, text, translate, visible
Shapes:
blending, data, edge-color, edge-color-cycle, edge-colormap,
edge-contrast-limits, edge-width, face-color, face-color-cycle,
face-colormap, face-contrast-limits, metadata, name, ndim, opacity,
properties, scale, shape-type, text, translate, visible, z-index
Surface:
blending, colormap, contrast-limits, data, gamma, metadata, name,
opacity, scale, translate, visible
Vectors:
blending, data, edge-color, edge-color-cycle, edge-colormap,
edge-contrast-limits, edge-width, length, metadata, name, opacity,
properties, scale, translate, visible

Thank you very much! :wink:

We don’t support that yet, but we could add a -f or --fullscreen option. cc @jni

Thank you very much @sofroniewn!. I vote +1 for that new features :slight_smile:
By the way, if I run the script as “napari example.py”, this opens one viewer as expected, but if I run the script as “python example.py”, this opens two viewers, one of them is totally empty and the other one shows the images. This discrepancy of usage is a little bit strange, is this the expected behaviour?

The code of the example.py script is the following:

import napari

with napari.gui_qt():

viewer = napari.Viewer()

layer1 = viewer.open('example_TE.zarr', layer_type='image', multiscale=True, contrast_limits=[0, 1], rgb=False)

layer2 = viewer.open('example_bin_labels.zarr', layer_type='labels', multiscale=True)

points = viewer.add_points(
    face_color='white', 
    edge_color='white', 
    size=100
)
points.mode='add'

Hmm no that’s no expected. If you comment out some lines in your example does it still happen? The viewer.open calls should all go to the only viewer you have already created.

Yeah I’m wondering if you can provide a minimal example that reproduces the problem with just generated data, rather than depending on your data files. (Or alternatively, provide the data files in a zip file, if they are reasonably sized…)

Thank you very much @sofroniewn and @jni,
This behavior is more strange than what I initially thought, the strange behavior only occurs when the filename of the script is napari_example.py. If I replace that name by simply example.py, it works as expected (only one viewer).


import numpy as np
import zarr
from skimage.transform import pyramid_gaussian
import napari

def generate_pyramid_zarr(fname, N=1000):

data = np.random.rand(N, N)

pyramid_data = list(
    pyramid_gaussian(data, downscale=2, max_layer=3, multichannel=False)
)

root = zarr.open_group(fname, mode='w')
for i in range(len(pyramid_data)):
    shape = pyramid_data[i].shape
    dtype = pyramid_data[i].dtype
    z = root.create_dataset(str(i), shape=shape, dtype=dtype, chunks=(300, 300))
    z[:] = pyramid_data[i]

generate_pyramid_zarr(‘example1.zarr’, N=1000)

with napari.gui_qt():

viewer = napari.Viewer()

viewer.open(
    'example1.zarr', 
    layer_type='image', 
    multiscale=True, 
    contrast_limits=[0, 1], 
    rgb=False
)

Could you please try to save this example as napari_example.py, and then run it as
python napari_example.py?

Could you please rename the file now as example.py and run it again as
python example.py?

Well that is interesting!! Thanks, I will try and reproduce.

Just as a reference, in case another beginner user like me is interesting in opening Napari in full-screen mode, you can do so as follows:

import sys
import napari
from qtpy.QtCore import Qt
from qtpy.QtWidgets import QApplication

app = QApplication()

viewer = napari.Viewer(show=False)
viewer.window._qt_window.setWindowState(Qt.WindowMaximized)
viewer.show()

sys.exit(app.exec_())

Please note that I am assuming the script does not receive any console inputs.

Ah if you want full screen mode from a script - not just the command line - then you can do

import napari

with napari.gui_qt():
    viewer=napari.Viewer()
    viewer.window._qt_window.showFullScreen()
    #viewer.window._qt_window.showNormal() # returns viewer to normal mode

In my original answer I was thinking you wanted a command line option, my apologies for not providing this answer sooner.

1 Like

Thank you very much @sofroniewn!, and please excuse me for my ambiguous question :blush: … Even though I am a beginner user, I like the project a lot!, I am digging into the source code to understand more and more the inner working of Napari, and while doing so I found a possible answer to my question, so I decided to share it here in the forum in case another beginner user was also looking for a similar answer ;).

Thank you very much for all your help!

1 Like