In my extension, I want to list all the files in a directory, and find ones with prefix name ‘jupyter’ (not in the original file system, it’s a main-area widget feature), how to do it with jupyterlab API?
You will want to hit the api/contents/ endpoint which will return JSON of the directory. It is not recursive but will tell you if each item is a file or a directory. It starts from your defined notebooks directory (such as /home/jovyan) and goes down.
api/contents/foo would then be the directory listing of //foo
could you please elaborate a bit more on the ‘api/contents’ API you’ve mentioned here? Is this a REST API or is it available in one of the libraries?
This is a rest endpoint. You can actually see it in use in the browser network tab any time you load a new directory in the file browser pain in JupyterLab. Also, be aware, that by default it does not include hidden files in the listing and the only way to change that is a Jupyter server configuration change (Can’t remember the setting off the top of my head)
Here is the full REST API documentation. Using makeRequest gives full access to various HTTP options, but the custom code would have to know what to do with the requests/payload, which are “Just JSON.”
On the client side, JupyterLab has a light asynchronous abstraction provided by the ContentsManager. This is mostly typed… but gremlins can always sneak in in an extensible system.
If extension needs grow substantially beyond this, a custom serverextension can also access the server’s ContentsManager: this is generally more robust than working directly with paths, as contents managers can store things in other places (e.g. s3, postgres, etc.) and expose its own API.