Skip to content

Commit

Permalink
Publish jupyterlab optuna
Browse files Browse the repository at this point in the history
  • Loading branch information
Alnusjaponica committed Jul 9, 2024
1 parent 4b2f831 commit fc09d8d
Show file tree
Hide file tree
Showing 19 changed files with 8,166 additions and 0 deletions.
95 changes: 95 additions & 0 deletions jupyerlab/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# jupyterlab-optuna

A JupyterLab extension for Optuna

## Requirements

- JupyterLab >= 4.0.0

## Install

To install the extension, execute:

```bash
pip install jupyterlab-optuna
```

## Uninstall

To remove the extension, execute:

```bash
pip uninstall jupyterlab-optuna
```

## Troubleshoot

If you are seeing the frontend extension, but it is not working, check
that the server extension is enabled:

```bash
jupyter server extension list
```

If the server extension is installed and enabled, but you are not seeing
the frontend extension, check the frontend extension is installed:

```bash
jupyter labextension list
```

## Contributing

### Development install

Note: You will need NodeJS to build the extension package.

The `jlpm` command is JupyterLab's pinned version of
[yarn](https://yarnpkg.com/) that is installed with JupyterLab. You may use
`yarn` or `npm` in lieu of `jlpm` below.

```bash
# Clone the repo to your local environment
# Change directory to the jupyterlab_optuna directory
# Install package in development mode
pip install -e ".[dev]"
# Link your development version of the extension with JupyterLab
jupyter labextension develop . --overwrite
# Server extension must be manually installed in develop mode
jupyter server extension enable jupyterlab_optuna
# Rebuild extension Typescript source after making changes
jlpm build
```

You can watch the source directory and run JupyterLab at the same time in different terminals to watch for changes in the extension's source and automatically rebuild the extension.

```bash
# Watch the source directory in one terminal, automatically rebuilding when needed
jlpm watch
# Run JupyterLab in another terminal
jupyter lab
```

With the watch command running, every saved change will immediately be built locally and available in your running JupyterLab. Refresh JupyterLab to load the change in your browser (you may need to wait several seconds for the extension to be rebuilt).

By default, the `jlpm build` command generates the source maps for this extension to make it easier to debug using the browser dev tools. To also generate source maps for the JupyterLab core extensions, you can run the following command:

```bash
jupyter lab build --minimize=False
```

### Development uninstall

```bash
# Server extension must be manually disabled in develop mode
jupyter server extension disable jupyterlab_optuna
pip uninstall jupyterlab_optuna
```

In development mode, you will also need to remove the symlink created by `jupyter labextension develop`
command. To find its location, you can run `jupyter labextension list` to figure out where the `labextensions`
folder is located. Then you can remove the symlink named `jupyterlab-optuna` within that folder.

### Packaging the extension

See [RELEASE](RELEASE.md)
111 changes: 111 additions & 0 deletions jupyerlab/RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Making a new release of jupyterlab_optuna

The extension can be published to `PyPI` and `npm` manually or using the [Jupyter Releaser](https://github.com/jupyter-server/jupyter_releaser).

## Manual release

### Python package

This extension can be distributed as Python packages. All of the Python
packaging instructions are in the `pyproject.toml` file to wrap your extension in a
Python package. Before generating a package, you first need to install some tools:

```bash
pip install build twine hatch
```

Bump the version using `hatch`. By default this will create a tag.
See the docs on [hatch-nodejs-version](https://github.com/agoose77/hatch-nodejs-version#semver) for details.

```bash
hatch version <new-version>
```

Make sure to clean up all the development files before building the package:

```bash
jlpm clean:all
```

You could also clean up the local git repository:

```bash
git clean -dfX
```

To create a Python source package (`.tar.gz`) and the binary package (`.whl`) in the `dist/` directory, do:

```bash
python -m build
```

> `python setup.py sdist bdist_wheel` is deprecated and will not work for this package.
Then to upload the package to PyPI, do:

```bash
twine upload dist/*
```

### NPM package

To publish the frontend part of the extension as a NPM package, do:

```bash
npm login
npm publish --access public
```

## Automated releases with the Jupyter Releaser

The extension repository should already be compatible with the Jupyter Releaser.

Check out the [workflow documentation](https://jupyter-releaser.readthedocs.io/en/latest/get_started/making_release_from_repo.html) for more information.

Here is a summary of the steps to cut a new release:

- Add tokens to the [Github Secrets](https://docs.github.com/en/actions/security-guides/encrypted-secrets) in the repository:
- `ADMIN_GITHUB_TOKEN` (with "public_repo" and "repo:status" permissions); see the [documentation](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token)
- `NPM_TOKEN` (with "automation" permission); see the [documentation](https://docs.npmjs.com/creating-and-viewing-access-tokens)
- Set up PyPI

<details><summary>Using PyPI trusted publisher (modern way)</summary>

- Set up your PyPI project by [adding a trusted publisher](https://docs.pypi.org/trusted-publishers/adding-a-publisher/)
- The _workflow name_ is `publish-release.yml` and the _environment_ should be left blank.
- Ensure the publish release job as `permissions`: `id-token : write` (see the [documentation](https://docs.pypi.org/trusted-publishers/using-a-publisher/))

</details>

<details><summary>Using PyPI token (legacy way)</summary>

- If the repo generates PyPI release(s), create a scoped PyPI [token](https://packaging.python.org/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/#saving-credentials-on-github). We recommend using a scoped token for security reasons.

- You can store the token as `PYPI_TOKEN` in your fork's `Secrets`.

- Advanced usage: if you are releasing multiple repos, you can create a secret named `PYPI_TOKEN_MAP` instead of `PYPI_TOKEN` that is formatted as follows:

```text
owner1/repo1,token1
owner2/repo2,token2
```

If you have multiple Python packages in the same repository, you can point to them as follows:

```text
owner1/repo1/path/to/package1,token1
owner1/repo1/path/to/package2,token2
```

</details>

- Go to the Actions panel
- Run the "Step 1: Prep Release" workflow
- Check the draft changelog
- Run the "Step 2: Publish Release" workflow

## Publishing to `conda-forge`

If the package is not on conda forge yet, check the documentation to learn how to add it: https://conda-forge.org/docs/maintainer/adding_pkgs.html

Otherwise a bot should pick up the new version publish to PyPI, and open a new PR on the feedstock repository automatically.
5 changes: 5 additions & 0 deletions jupyerlab/install.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"packageManager": "python",
"packageName": "jupyterlab_optuna",
"uninstallInstructions": "Use your Python package manager (pip, conda, etc.) to uninstall the package jupyterlab_optuna"
}
7 changes: 7 additions & 0 deletions jupyerlab/jupyter-config/server-config/jupyterlab_optuna.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ServerApp": {
"jpserver_extensions": {
"jupyterlab_optuna": true
}
}
}
36 changes: 36 additions & 0 deletions jupyerlab/jupyterlab_optuna/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
try:
from ._version import __version__
except ImportError:
# Fallback when using the package in dev mode without installing
# in editable mode with pip. It is highly recommended to install
# the package from a stable release or in editable mode: https://pip.pypa.io/en/stable/topics/local-project-installs/#editable-installs
import warnings
warnings.warn("Importing 'jupyterlab_optuna' outside a proper installation.")
__version__ = "dev"
from .handlers import setup_handlers


def _jupyter_labextension_paths():
return [{
"src": "labextension",
"dest": "jupyterlab-optuna"
}]


def _jupyter_server_extension_points():
return [{
"module": "jupyterlab_optuna"
}]


def _load_jupyter_server_extension(server_app):
"""Registers the API handler to receive HTTP requests from the frontend extension.
Parameters
----------
server_app: jupyterlab.labapp.LabApp
JupyterLab application instance
"""
setup_handlers(server_app.web_app)
name = "jupyterlab_optuna"
server_app.log.info(f"Registered {name} server extension")
94 changes: 94 additions & 0 deletions jupyerlab/jupyterlab_optuna/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from __future__ import annotations

import json
import threading
from typing import TYPE_CHECKING

import tornado
from jupyter_server.base.handlers import APIHandler
from jupyter_server.utils import url_path_join
from optuna_dashboard.artifact._backend_to_store import to_artifact_store

try:
from optuna.artifacts import FileSystemArtifactStore
except ImportError:
from optuna_dashboard.artifact.file_system import \
FileSystemBackend as FileSystemArtifactStore

from optuna_dashboard import wsgi
from tornado.web import FallbackHandler
from tornado.wsgi import WSGIContainer

if TYPE_CHECKING:
from _typeshed.wsgi import WSGIApplication


API_NAMESPACE = "jupyterlab-optuna"

_dashboard_app: WSGIApplication | None = None
_is_initialized = False
threading_lock = threading.Lock()


class RouteHandler(APIHandler):
@tornado.web.authenticated
def post(self):
global _dashboard_app, _is_initialized

input_data = self.get_json_body()
storage_url = input_data.get("storage_url")
artifact_path = input_data.get("artifact_path")

if storage_url is None:
self.set_status(400)
self.finish(json.dumps({"reason": "storage_url is required"}))
return

if artifact_path:
artifact_store = to_artifact_store(FileSystemArtifactStore(artifact_path))
else:
artifact_store = None

with threading_lock:
_dashboard_app = wsgi(storage=storage_url, artifact_store=artifact_store)
_is_initialized = True
self.finish(json.dumps({"is_initialized": True}))


class InitializedStateHandler(APIHandler):
@tornado.web.authenticated
def get(self):
self.finish(json.dumps({"is_initialized": _is_initialized}))


def dashboard_app(env, start_response):
# Set Content-Type
if "/api/" in env["PATH_INFO"]:
env["CONTENT_TYPE"] = "application/json"
env["PATH_INFO"] = env["PATH_INFO"].replace(f"/{API_NAMESPACE}", "")

if _dashboard_app is None:
start_response("400 Bad Request", [{"Content-Type": "application/json"}])
return [b'{"reason": "app is not initialized"}']

return _dashboard_app(env, start_response)


def setup_handlers(web_app):
host_pattern = ".*$"

base_url = web_app.settings["base_url"]
# Prepend the base_url so that it works in a JupyterHub setting
initialize_route_pattern = url_path_join(base_url, API_NAMESPACE, "api/is_initialized")
handlers = [(initialize_route_pattern, InitializedStateHandler)]
web_app.add_handlers(host_pattern, handlers)

resister_route_pattern = url_path_join(base_url, API_NAMESPACE, "api/register_dashboard_app")
handlers = [(resister_route_pattern, RouteHandler)]
web_app.add_handlers(host_pattern, handlers)

route_pattern = url_path_join(base_url, API_NAMESPACE, r"(.*)")
handlers = [
(route_pattern, FallbackHandler, dict(fallback=WSGIContainer(dashboard_app))),
]
web_app.add_handlers(host_pattern, handlers)
Loading

0 comments on commit fc09d8d

Please sign in to comment.