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

On notebook download, custom ContentsManager raises StaticFileHandler.initialize() exception #1313

Open
miraculixx opened this issue Aug 12, 2023 · 4 comments
Labels

Comments

@miraculixx
Copy link

miraculixx commented Aug 12, 2023

Description

When implementing a custom ContentsManager, jupyter_server raises the following exception upon downloading files from Jupyter Lab, and the file will not be downloaded.

TypeError: StaticFileHandler.initialize() missing 1 required positional argument: 'path'

Reproduce

Implement a custom ContentsManager according to the guideline as documented here.

  1. Run Jupyter Lab with the c.ServerApp.contents_manager_class=module.CustomContentsManager
  2. Create and select a notebook, Go to File > Download
  3. The file will not be downloaded, and the console will show the above exception

Expected behavior

The file is downloaded and no exception is raised.

Context

  • Operating System and version: Linux Mint 21.1 Vera
  • Browser and version: Google Chrome 115.0.5790.170
  • Jupyter Server version: 2.7.0

This used to work (i.e. did not raise an exception) in versions jupyter_server<2, however I did not investigate the exact version that changed the behavior.

Troubleshoot Output
$ jupyter troubleshoot | grep -iE "jupyter-server|jupyterlab|nbclassic|notebook"
        jupyterlab                 4.0.4
        jupyterlab-pygments        0.2.2
        jupyterlab_server          2.24.0
        nbclassic                  1.0.0
        notebook                   7.0.2
        notebook_shim              0.2.3
        jupyter-server            2.7.0                    pypi_0    pypi
        jupyter-server-terminals  0.4.4                    pypi_0    pypi
        jupyterlab                4.0.4                    pypi_0    pypi
        jupyterlab-pygments       0.2.2                    pypi_0    pypi
        jupyterlab-server         2.24.0                   pypi_0    pypi
        nbclassic                 1.0.0                    pypi_0    pypi
        notebook                  7.0.2                    pypi_0    pypi
        notebook-shim             0.2.3                    pypi_0    pypi
              - jupyter-server==2.7.0
              - jupyter-server-terminals==0.4.4
              - jupyterlab==4.0.4
              - jupyterlab-pygments==0.2.2
              - jupyterlab-server==2.24.0
              - nbclassic==1.0.0
              - notebook==7.0.2
              - notebook-shim==0.2.3
Command Line Output
[I 2023-08-12 13:39:09.343 ServerApp] nbclassic | extension was successfully loaded.                                                                                                          
[I 2023-08-12 13:39:09.346 ServerApp] notebook | extension was successfully loaded.                                                                                                           
[I 2023-08-12 13:39:09.347 ServerApp] Serving contents                                                                                                                                        
[I 2023-08-12 13:39:09.347 ServerApp] Jupyter Server 2.7.0 is running at:                                                                                                                     
[I 2023-08-12 13:39:09.347 ServerApp] http://localhost:8888/lab?token=77428c315021ebc6de85ce58f0d17c82f263b4f50ed5b57c                                                                        
[I 2023-08-12 13:39:09.347 ServerApp]     http://127.0.0.1:8888/lab?token=77428c315021ebc6de85ce58f0d17c82f263b4f50ed5b57c                                                                    
[I 2023-08-12 13:39:09.347 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
(...)
[E 2023-08-12 13:39:20.279 ServerApp] Uncaught exception                                                                                                                                      
    Traceback (most recent call last):                                                                                                                                                        
      File "/home/user/miniforge3/envs/omegaml-ce/lib/python3.10/site-packages/tornado/http1connection.py", line 276, in _read_message                                                     
        delegate.finish()                                                                                                                                                                     
      File "/home/user/miniforge3/envs/omegaml-ce/lib/python3.10/site-packages/tornado/routing.py", line 268, in finish                                                                    
        self.delegate.finish()                                                                                                                                                                
      File "/home/user/miniforge3/envs/omegaml-ce/lib/python3.10/site-packages/tornado/web.py", line 2398, in finish                                                                       
        self.execute()                                                                                                                                                                        
      File "/home/user/miniforge3/envs/omegaml-ce/lib/python3.10/site-packages/tornado/web.py", line 2420, in execute                                                                      
        self.handler = self.handler_class(                                                                                                                                                    
      File "/home/user/miniforge3/envs/omegaml-ce/lib/python3.10/site-packages/tornado/web.py", line 239, in __init__                                                                      
        self.initialize(**kwargs)  # type: ignore                                                                                                                                             
    TypeError: StaticFileHandler.initialize() missing 1 required positional argument: 'path'
Browser Output
(no relevant console output)
@miraculixx miraculixx added the bug label Aug 12, 2023
@welcome
Copy link

welcome bot commented Aug 12, 2023

Thank you for opening your first issue in this project! Engagement like this is essential for open source projects! 🤗

If you haven't done so already, check out Jupyter's Code of Conduct. Also, please try to follow the issue template as it helps other other community members to contribute more effectively.
welcome
You can meet the other Jovyans by joining our Discourse forum. There is also an intro thread there where you can stop by and say Hi! 👋

Welcome to the Jupyter community! 🎉

@miraculixx
Copy link
Author

miraculixx commented Aug 12, 2023

The following work around seems the fix the immediate issue, however I am not sure of the implications of this.

In your custom ContentsManager, add the following.

class CustomContentsManager(ContentsManager):
     .... 
    @default('files_handler_params')
    def _files_handler_params_default(self):
        return {'path': self.root_dir}

Alternatively, add the following in your jupyter_server_config.py

c.ContentsManager.file_handler_params = { 'path': '/valid/path' }

What this does

  • ensure that ContentsManager.files_handler_class=FilesHandler gets has a path attribute on initialize()
  • this only happens when dealing with local files, e.g. when downloading a notebook
  • source of the issue is that ContentsManager.files_handler_params is not set

@fakhavan
Copy link

I've faced the same error while using a hybridcontents content manager to allow both LargeFileManager and S3Contents in our jupyter hub deployment with helm.
I tried applying the suggestion from @miraculixx by passing on files_handler_params into the LargeFileManager manager class like this:

## Contents of `jupyter_notebook_config.py` file


HOME_DIR = path.join("/home", environ["NB_USER"])
# Configure the variables for s3contents library for shared space


# Configure HybridContentsManager to use LargeFileManager for root dir and personal-space and S3ContentsManager for s3-shared-space
c.HybridContentsManager.manager_classes = {
    # making the root dir read-only to contain guides and other read-only content
    "": LargeFileManager,
    # making the personal space read-write into an EBS volume
    "personal-space": LargeFileManager,
    # making the shared space read-write into an S3 bucket
    "shared-space": S3ContentsManager,
}

c.HybridContentsManager.manager_kwargs = {
    # using /tmp/read-only as the root dir, which is read-only, and contains guides that users would see when they open JupyterHub
    "": {"root_dir": READ_ONLY_ROOT_DIR},
    # using the user's home dir as the personal space, and is mounted to an EBS volume which is presisted across sessions
    "personal-space": {"root_dir": HOME_DIR, "files_handler_params": {"path": HOME_DIR}},
    # using the S3 bucket as the shared space, and is mounted to an S3 bucket which is presisted across sessions
    "shared-space": {
        "endpoint_url": S3_ENDPOINT_URL,
        "bucket": S3_BUCKET_NAME,
        "region_name": S3_REGION_NAME,
    },
}

# To prevent `directory not found ""` error when opening JupyterLab
c.ServerApp.root_dir = HOME_DIR

The above code is amended into jupyter_notebook_config.py file on jupyterhub server startup, however, this does not solve my issue. I assume the files_handler_params is not passed in the right format or is not being added into to the right file i.e. jupyter_notebook_config.py instead of jupyter_server_config.py, however I'm not sure what's the correct way to handle the issue with my setup.
I am wondering if anyone has run into similar issues when using other content managers like HybridContents or has any idea on how to solve this.

Cheers

@vlobachevsky
Copy link

@fakhavan I was able to resolve this by setting files_handler_params in HybridContentsManager:

c.HybridContentsManager.files_handler_params = { 'path': HOME_DIR }

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

No branches or pull requests

3 participants