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

Make ExtensionHandler a Mixin #174

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/source/frontends.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Writing a frontend application
Jupyter Server provides two key classes for writing a server frontend:

- ``ExtensionApp``
- ``ExtensionHandler``
- ``ExtensionHandlerMixin``

The ExtensionApp:

Expand Down Expand Up @@ -81,13 +81,13 @@ Properties
Writing frontend handlers
-------------------------

To write handlers for an ``ExtensionApp``, use the ``ExtensionHandler`` class. This class routes Tornado's ``static_url`` attribute to the ``/static/<extension_name>/`` namespace where your frontend's static files will be served.
To write handlers for an ``ExtensionApp``, use the ``ExtensionHandlerMixin`` class. This class routes Tornado's ``static_url`` attribute to the ``/static/<extension_name>/`` namespace where your frontend's static files will be served.

.. code-block:: python

from jupyter_server.extension import ExtensionHandler
from jupyter_server.extension import ExtensionHandlerMixin

class MyFrontendHandler(ExtensionHandler):
class MyFrontendHandler(ExtensionHandlerMixin, JupyterHandler):

urls = ['/myfrontend/hello']

Expand All @@ -97,7 +97,7 @@ To write handlers for an ``ExtensionApp``, use the ``ExtensionHandler`` class. T
def post(self):
...

ExtensionHandler comes with the following properties:
ExtensionHandlerMixin comes with the following properties:

* ``config``: the ExtensionApp's config object.
* ``server_config``: the ServerApp's config object.
Expand Down
4 changes: 2 additions & 2 deletions jupyter_server/extension/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from jupyter_server.serverapp import ServerApp, aliases, flags
from jupyter_server.transutils import _
from jupyter_server.utils import url_path_join
from .handler import ExtensionHandler
from .handler import ExtensionHandlerMixin

# Remove alias for nested classes in ServerApp.
# Nested classes are not allowed in ExtensionApp.
Expand Down Expand Up @@ -321,7 +321,7 @@ def _prepare_handlers(self):

# Get handler kwargs, if given
kwargs = {}
if issubclass(handler, ExtensionHandler):
if issubclass(handler, ExtensionHandlerMixin):
kwargs['extension_name'] = self.extension_name
try:
kwargs.update(handler_items[2])
Expand Down
4 changes: 2 additions & 2 deletions jupyter_server/extension/handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from jupyter_server.base.handlers import JupyterHandler, FileFindHandler
from jupyter_server.base.handlers import FileFindHandler
from traitlets import Unicode, default


Expand All @@ -12,7 +12,7 @@ def get_template(self, name):
return self.settings[env].get_template(name)


class ExtensionHandler(JupyterHandler):
class ExtensionHandlerMixin():
"""Base class for Jupyter server extension handlers.

Subclasses can serve static files behind a namespaced
Expand Down
7 changes: 4 additions & 3 deletions tests/extension/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@


from jupyter_core import paths
from jupyter_server.base.handlers import JupyterHandler
from jupyter_server.extension import serverextension
from jupyter_server.extension.serverextension import _get_config_dir
from jupyter_server.extension.application import ExtensionApp, ExtensionAppJinjaMixin
from jupyter_server.extension.handler import ExtensionHandler, ExtensionHandlerJinjaMixin
from jupyter_server.extension.handler import ExtensionHandlerMixin, ExtensionHandlerJinjaMixin

# ----------------- Mock Extension App ----------------------

class MockExtensionHandler(ExtensionHandler):
class MockExtensionHandler(ExtensionHandlerMixin, JupyterHandler):

def get(self):
self.finish(self.config.mock_trait)


class MockExtensionTemplateHandler(ExtensionHandlerJinjaMixin, ExtensionHandler):
class MockExtensionTemplateHandler(ExtensionHandlerJinjaMixin, ExtensionHandlerMixin, JupyterHandler):

def get(self):
self.write(self.render_template("index.html"))
Expand Down