Jupyter Notebook - A web-based notebook environment for interactive computing
—
The web request handlers manage HTTP requests for the different notebook interfaces, including the file browser, notebook editor, console, terminal, and file editor. These handlers serve the web-based user interface and manage the notebook's web application functionality.
The NotebookBaseHandler provides common functionality for all notebook web handlers, including page configuration and template rendering.
class NotebookBaseHandler(ExtensionHandlerJinjaMixin, ExtensionHandlerMixin, JupyterHandler):
"""
The base notebook API handler.
Provides common functionality for all notebook web handlers including
page configuration, template rendering, and authentication.
"""
@property
def custom_css(self) -> Any:
"""
Whether custom CSS is enabled.
Returns:
Any: Custom CSS setting from application configuration
(returns self.settings.get("custom_css", True))
"""
def get_page_config(self) -> dict[str, Any]:
"""
Get the page configuration for the web interface.
Builds complete page configuration including application metadata,
URLs, authentication tokens, frontend settings, and JupyterHub
integration if applicable.
Returns:
dict[str, Any]: Complete page configuration for frontend
"""The TreeHandler manages requests for the file browser interface, handling directory listings and file navigation.
class TreeHandler(NotebookBaseHandler):
"""
A tree page handler for the file browser interface.
Handles requests for directory listings and file navigation,
redirecting to appropriate editors based on file type.
"""
@web.authenticated
async def get(self, path: str = "") -> None:
"""
Display appropriate page for given path.
Behavior depends on path type:
- Directory: Shows directory listing/tree view
- Notebook file: Redirects to notebook editor
- Other files: Redirects to raw file view
Parameters:
- path: str, file system path relative to server root
Returns:
None: Renders template or performs redirect
Raises:
HTTPError: 404 if path doesn't exist or is hidden
"""The NotebookHandler manages requests for notebook document editing interface.
class NotebookHandler(NotebookBaseHandler):
"""
A notebook page handler for the notebook editing interface.
Handles requests for notebook documents, providing the main
notebook editing environment.
"""
@web.authenticated
async def get(self, path: str = "") -> Any:
"""
Get the notebook page or redirect if path is a directory.
Parameters:
- path: str, notebook file path relative to server root
Returns:
Any: Rendered notebook template or redirect response
If the path is a directory, redirects to tree view.
Otherwise renders the notebook editing interface.
"""The ConsoleHandler manages requests for the console/kernel interface.
class ConsoleHandler(NotebookBaseHandler):
"""
A console page handler for interactive console interface.
Provides access to interactive Python/kernel consoles
for code execution and debugging.
"""
@web.authenticated
def get(self, path: str | None = None) -> Any:
"""
Get the console page.
Parameters:
- path: str | None, optional path parameter (unused)
Returns:
Any: Rendered console template
Renders the console interface template with current
page configuration.
"""The TerminalHandler manages requests for the terminal interface.
class TerminalHandler(NotebookBaseHandler):
"""
A terminal page handler for system terminal access.
Provides web-based terminal access for command line
operations and system administration.
"""
@web.authenticated
def get(self, path: str | None = None) -> Any:
"""
Get the terminal page.
Parameters:
- path: str | None, optional path parameter (unused)
Returns:
Any: Rendered terminal template
Renders the terminal interface template with current
page configuration.
"""The FileHandler manages requests for the file editing interface.
class FileHandler(NotebookBaseHandler):
"""
A file page handler for text file editing interface.
Provides text editor functionality for non-notebook files
including source code, configuration files, and documentation.
"""
@web.authenticated
def get(self, path: str | None = None) -> Any:
"""
Get the file editor page.
Parameters:
- path: str | None, optional file path parameter (unused in current implementation)
Returns:
Any: Rendered file editor template
Renders the file editing interface template with current
page configuration.
"""The CustomCssHandler serves custom CSS files for interface customization.
class CustomCssHandler(NotebookBaseHandler):
"""
A custom CSS handler for serving user customizations.
Serves custom CSS files to allow users to customize
the notebook interface appearance.
"""
@web.authenticated
def get(self) -> Any:
"""
Get the custom CSS file.
Returns:
Any: CSS file content with appropriate content type
Serves custom.css from the Jupyter configuration directory
or static directory if available. Sets appropriate CSS
content type header.
"""from notebook.app import JupyterNotebookApp
# Handlers are automatically registered by JupyterNotebookApp
app = JupyterNotebookApp()
app.initialize_handlers()
# The following URL patterns are registered:
# - "/tree(.*)" -> TreeHandler
# - "/notebooks(.*)" -> NotebookHandler
# - "/edit(.*)" -> FileHandler
# - "/consoles/(.*)" -> ConsoleHandler
# - "/terminals/(.*)" -> TerminalHandler
# - "/custom/custom.css" -> CustomCssHandlerfrom notebook.app import NotebookBaseHandler
class CustomHandler(NotebookBaseHandler):
def get(self):
# Access page configuration
config = self.get_page_config()
# Configuration includes:
# - appVersion: Application version
# - baseUrl: Base URL for the server
# - token: Authentication token
# - terminalsAvailable: Whether terminals are enabled
# - preferredPath: Default directory path
# - Custom CSS settings
# - JupyterHub integration settings (if applicable)
return self.render_template("custom.html", page_config=config)from tornado import web
from notebook.app import NotebookBaseHandler
class SecureHandler(NotebookBaseHandler):
@web.authenticated # Requires user authentication
async def get(self, path: str = ""):
try:
# Check if path exists and is accessible
cm = self.contents_manager
if await cm.dir_exists(path=path):
# Handle directory
if await cm.is_hidden(path) and not cm.allow_hidden:
raise web.HTTPError(404, "Directory not found")
# Render directory view
elif await cm.file_exists(path):
# Handle file
# Render file view or redirect
else:
raise web.HTTPError(404, "Path not found")
except Exception as e:
# Handle errors appropriately
raise web.HTTPError(500, str(e))Install with Tessl CLI
npx tessl i tessl/pypi-notebook