Google collab : protect some block and not others

First time using Google collab here.

I would like to share a python code for data analysis, which would be used by non-coders. I’d like to import a function in collab, one that couldn’t be modified by the users (but could be viewed and copy-pasted if they wanna modify parameters) . But they would have the possibility to enter their data and call that function.

I tried importing the function as a file, but then other users don’t seem to have access to it when they try to call it. So I add the function code in a block a the beginning of the document, but I can’t find a way to protect the code.

The thing is, I’d need in the same document a block protected (the function) and one unprotected (the one where they would put their data and call the function).

Any idea if / how I can do that?

Thank you!!

There are three common ways you would do that:

  1. Implement a library that has that function, install it (using setup.py or any other packaging method), and import it at the top of the document.
  2. Add that function definition into the file that executes when the kernel is initialized (sadly, I do not remember the path to the file and I do not know whether google collab gives you that functionality but I know for a fact that you can do this in a regular jupyter notebook server)
  3. Encode that python code in base64 and use exec on it in the top cells as described here

All of these methods will make it harder for the users to edit the code of your function while also allowing them to use it. However I do not know and do not think that there exist any ways of actually prohibiting the users from modifying or overriding your function (except for modifying Cpython or IPython themselves or creating a jupyter extension that tracks the modification of certain identifiers).

I only recommend the first bulletpoint as the other two are quite hacky in your use case.

Thank you !

I’m sorry, I’m a bit of a noob. I should have precised that the function has been written by myself, so it isn’t in any library. Or maybe I should create a “public library” and then upload it on colab ?

Yeah, the other two seem a bit complicated as a beginner :upside_down_face:

If you are not confident in your skills (no problem with that :)), then I wouldn’t want to dump the packaging on you just yet. So let’s use another solution:

Is it possible for you to put your function in a read-only file (i.e. the users won’t be able to write it) and import it? You mentioned that it didn’t work for you for some reason – most likely because your python file wasn’t on sys.path. So let me show you two other ways to solve your problem:

  1. Put the python file you wish to import in a correct location that is on sys.path as described here (Though I wouldn’t recommend using open("myfile.py", "wb").write. I would suggest using pathlib.Path.write_text instead.
  2. Add your python file to sys.path like so:
import sys
sys.path.append("path_to_directory_containing_your_python_file")
import your_python_file

I don’t know what went wrong, I can’t seem to make it work ahah.
I guess I’ll just leave the document on “read-only” and ask for the users to copy-past the function on their own notebook.
Anyway, thank you so much for your help, that was very kind !

1 Like