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

Readonly notebook #3251

Open
zvikagart opened this issue Nov 16, 2017 · 8 comments
Open

Readonly notebook #3251

zvikagart opened this issue Nov 16, 2017 · 8 comments

Comments

@zvikagart
Copy link

The goal is to render a notebook but prevent editing.
I've tried passing readOnly: true to NotebookWidgetFactory but that doesn't seem to have any effect. I can see in the code that a readonly flag is being passed around and applied to cells, but I haven't been able to enable this feature. I also didn't find anything in the documentation.

@blink1073
Copy link
Member

Hi @zvikagart, you should be able to set the notebook model to readOnly. You can do this by setting context.model.readOnly in the createNew method.

@ian-r-rose
Copy link
Member

I suspect that @zvikagart is referring to making the codemirror instances readOnly. Setting the readOnly flag on the notebook currently seems to only affect whether JupyterLab attempts to save the file to disk: it does not prevent in-memory edits of the notebook.

I think this behavior is confusing. It can be useful in some instances, however. In the @jupyterlab/github extension, it allows for running of a notebook loaded from a repository without having write access to said repository. Perhaps we can make some further distinction between read-only from the server's perspective vs read-only in the UI?

@blink1073
Copy link
Member

Ah, yep, that makes sense.

@blink1073 blink1073 modified the milestones: Reference, 1.0 Nov 20, 2017
@zvikagart
Copy link
Author

Correct, I want to prevent cells from entering edit mode, but also prevent other edits to the notebook like adding/deleting cells, moving cells around, etc.

@zvikagart
Copy link
Author

zvikagart commented Nov 27, 2017

I use the following code to hide the toolbar and set codemirror cells to readOnly. It prevents edits but still allows cells to go into edit mode, which is acceptable but not perfect.

nbWidget.toolbar.hide();
each(nbWidget.notebook.children(), widget => widget.readOnly = true);

I also disable execution of commands that modify the notebook. For example:

isEnabled = () => !readOnly;

commands.addCommand(cmdIds.save, {
  label: 'Save',
  execute: () => nbWidget.context.save(),
  isEnabled: isEnabled
});

commands.addCommand(cmdIds.split, {
  label: 'Split Cell',
  execute: () => NotebookActions.splitCell(nbWidget.notebook),
  isEnabled: isEnabled
});

@beledouxdenis
Copy link

beledouxdenis commented Jun 5, 2018

I landed on this topic while I was looking for making the file editor read-only when the filesystem was mounted read-only, and I wanted to share my findings.

This is my first time digging into the source of Jupyterlab and I must say it was quite instructing.

Basically, I was able to make the file editor read-only when the file opened was not writable by adding the below line:

editor.setOption('readOnly', !this.context.contentsModel.writable);

in the method _onContextReady of file packages/fileeditor/src/widget.ts

Also, the save button remains enabled and you therefore still have the "permission denied" when you try to use it (still in the context of a read-only filesystem). I saw there is a nicer message when the context model was flagged as readOnly, and I therefore thought of using it as well by flagging the context model as readOnly in the same location:

contextModel.readOnly = !this.context.contentsModel.writable;

That being said, I am not sure you expect from a file editor to be read-only or to disable the save button when the file is read-only. Indeed, when opening a read-only file regular file editor (Gedit, Sublime Text, Atom, ...):

  • you can still edit the file, and it makes sense as you can make changes and save the file in another, writable, location (Save as),
  • you can still hit the regular Save button. And when you do use it, you have as well a Permission denied error. See below for an illustration with Atom. I guess this is done in a user-friendly context: Better let the user try to click on a button and tell him why he cannot rather than disabling the button without any explanation.
    screenshot from 2018-06-05 17-26-12

My conclusion, in my opinion, is that changing these behaviors should rather be done in the context of a specific extension/add-on, for instance by overriding _onContextReady and adding the lines I mention above, rather than making them standard.

The error message Permission denied versus Cannot Save. Document is read-only is discussable. I myself believe Permission denied is more accurate: It's not because the current user cannot write on the file that another cannot.

This is for the file editor rather than the notebook editor, but I think the same rules/behavior applies.

@jasongrout jasongrout removed this from the 1.0 milestone Sep 5, 2018
@blink1073 blink1073 added this to the Future milestone Sep 11, 2018
@futurist
Copy link
Contributor

@zvikagart If you want prevent editing, just override the IDrive.get method, to set metadata.editable to false, like below:

class ReadonlyDrive extends Drive implements Contents.IDrive {
    name = 'readonly';
    async get(
      path: string,
      options?: Contents.IFetchOptions
    ): Promise<Contents.IModel> {
        // ... get file content
        content.cells.forEach((cell: any) => {
            cell.metadata = cell.metadata || {};
            cell.metadata.editable = false;
        });
        return {
            name, path, content, ...
        } as Contents.IModel;
    }
}

This way everything will be readOnly.

@futurist
Copy link
Contributor

I think we need a option in SessionManager or ServiceManager, if the whole notebook is readOnly and just for preview, do not need to connect to a kernel, and no listRunning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants