Creating new files with custom ContentsManager

Hello.

I’m writing a custom ContentsManager manager for Jupyter Notebooks and gotten to creating new files.

Originally my file_exists function always returned True and I’ve gotten Cannot POST to files, use PUT instead error, same as this issue.

Following that I amended file_exists function to return False, however now Jupyter is stuck in the loop calling file_exists with:

/dir
/dir/Untitled.ipynb
/dir/Untitled1.ipynb
/dir/Untitled2.ipynb
...

My question is - what should I return from file_exists so that notebook creation goes along?

I though maybe get or save get called after file_exists but that doesn’t seem to be the case.

Is your file_exists method always returning a fixed value? What happens if you change it so it returns True if the file exists and False if it doesn’t?

Originally, yes, but now I’m just trying to understand what it must return so file creation goes along.

I thought so at first, but Jupyter seems to check file_exists once per file.
The sequence seem not to be:

check if file exist
do something
check if file exist again

But rather:

check if file exist
check if file2 exist
....

So I don’t understand what must I answer on a first file_exist.

I recently wrote my own ContentsManager and the file_exists method is implemented as you’d expect. Is your code visible in a public repository?

Notebook creation fails through. I think notebook creation happens here and it seems like file_exists should always return true and dir_exists should always return false yet this configuration causes infinite loop described above.

What are you using for your Checkpoints? When I wrote my ContentsManager I also needed to write a Checkpoints class before saving notebooks would work properly.

Ooh, I’m not using checkpoints for now. Is it a requirement?

I’ll write a small checkpoints wrapper and see if it helps.

Thanks for the idea.

Docs don’t specify that checkpoints class is required,so I thought they are optional.

I’m not sure if it’s strictly required, it might be possible to disable it but I couldn’t figure out how so ended up writing my own.

@manics checkpoints don’t matter, but thank you for the advice. Here’s the answer:
exist - false
exist_dir for dir - true
exist_dir for file - false
That’s the ticket

1 Like