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

adds exhibits_url and related functionality #50

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion jupyterlab_gallery/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def get(self):

class ExhibitsHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
async def get(self):
await self.gallery_manager.get_exhibit_url_data()
self.finish(
json.dumps(
{
Expand Down
24 changes: 24 additions & 0 deletions jupyterlab_gallery/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from pathlib import Path
from typing import Optional
from threading import Thread
import json

from traitlets.config.configurable import LoggingConfigurable
from traitlets import Dict, List, Unicode, Bool, Int
from tornado.httpclient import AsyncHTTPClient

from .git_utils import (
extract_repository_owner,
Expand Down Expand Up @@ -63,6 +65,13 @@ def __init__(self, *args, **kwargs):
default_value=[],
)

exhibits_url = Unicode(
help="A url to a json or yaml file containing exhibits",
default_value=None,
allow_none=True,
config=True
)

destination = Unicode(
help="The directory into which the exhibits will be cloned",
default_value="gallery",
Expand Down Expand Up @@ -93,6 +102,21 @@ def _check_updates(self, exhibit):
):
self._has_updates[local_path] = has_updates(local_path)

async def get_exhibit_url_data(self):
if self.exhibits_url:
http_client = AsyncHTTPClient()
response = await http_client.fetch(self.exhibits_url)
if not response.code == 200:
raise RuntimeError(
f"Unable to fetch exhibits from {self.exhibits_url}"
)
content = json.loads(response.body)
content_exhibits = content.get("exhibits", [])
for exhibit in content_exhibits:
git = exhibit["git"]
if not any(e["git"] == git for e in self.exhibits):
self.exhibits.append(exhibit)

def get_exhibit_data(self, exhibit):
data = {}

Expand Down
36 changes: 36 additions & 0 deletions jupyterlab_gallery/tests/test_handlers.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@
import json
from unittest import mock
import pytest
import tornado
import json

from jupyter_server.utils import url_path_join

from jupyterlab_gallery.manager import GalleryManager


@pytest.fixture
def exhibits_server(jp_asyncio_loop):
async def run_server():
content = {"exhibits": [
{
"git": "https://github.com/nebari-dev/jupyterlab-gallery.git",
"homepage": "https://github.com/nebari-dev/nebari",
"title": "jupyterlab-gallery"
}
]}

class ExhibitsJsonHandler(tornado.web.RequestHandler):
def get(self):
self.write(content)

app = tornado.web.Application([(r"/exhibits.json", ExhibitsJsonHandler)])

server = app.listen(8030)
return server

server = jp_asyncio_loop.run_until_complete(run_server())
yield "http://127.0.0.1:8030"
server.stop()


async def test_exhibits(jp_fetch):
response = await jp_fetch("jupyterlab-gallery", "exhibits")
assert response.code == 200
payload = json.loads(response.body)
assert isinstance(payload["exhibits"], list)

async def test_exhibits_with_exhibits_url(jp_fetch, exhibits_server):
url = exhibits_server
with mock.patch.object(GalleryManager, "exhibits_url", url + "/exhibits.json"):
response = await jp_fetch("jupyterlab-gallery", "exhibits")
assert response.code == 200
payload = json.loads(response.body)
assert len(payload["exhibits"]) == 1
assert payload["exhibits"][0]["title"] == "jupyterlab-gallery"


@pytest.mark.parametrize(
"exhibit",
Expand Down
Loading