Skip to content

Commit

Permalink
feat(notebookviewer): upgrade dependencies to resolve ECR scan findings
Browse files Browse the repository at this point in the history
  • Loading branch information
benforshey committed Dec 20, 2024
1 parent 21166da commit 31caa02
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 24 deletions.
7 changes: 4 additions & 3 deletions packages/notebook-viewer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
ARG REGISTRY
FROM ${REGISTRY}python:3.9.1
FROM ${REGISTRY}python:3.13.1-alpine

RUN mkdir -p /usr/src/app
RUN apt-get update
RUN apk update
RUN apk upgrade

WORKDIR /usr/src/app

COPY . .

RUN pip install -r requirements.txt

CMD python -m flask run --host=0.0.0.0 --port 8080
CMD ["gunicorn", "-b", "0.0.0.0:8080", "app:app"]

EXPOSE 8080
3 changes: 3 additions & 0 deletions packages/notebook-viewer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# mEditor Notebook Viewer

This Flask API expects receives a NASA-hosted Jupyter Notebook URL at `/meditor/notebookviewer/?notebookUrl={URL}` and renders the static Jupyter Notebook. It's used to serve content on the GES DISC website.
35 changes: 21 additions & 14 deletions packages/notebook-viewer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
from lib.html_exporter import html_exporter

# only notebooks from these domains are allowed
DOMAIN_WHITELIST_REGEX = "^https://([a-zA-Z0-9]+\.)*(nasa\.gov|github\.com|githubusercontent\.com)\/?.*"
DOMAIN_WHITELIST_REGEX = (
r"^https://([a-zA-Z0-9]+\.)*(nasa\.gov|github\.com|githubusercontent\.com)\/?.*"
)

app = Flask("mEditor Notebook Viewer")

app = Flask('mEditor Notebook Viewer')

@app.route("/meditor/notebookviewer/")
def getNotebookAsHtml():
Expand All @@ -17,39 +20,43 @@ def getNotebookAsHtml():

def convertNotebookToHtml():
# ensure the user passed in a notebook url
if request.args.get('notebookUrl') == None:
if request.args.get("notebookUrl") is None:
return "Missing a required URL parameter, `notebookUrl`", 400

notebookUrl = urllib.parse.unquote(request.args.get('notebookUrl'))
notebookUrl = urllib.parse.unquote(request.args.get("notebookUrl"))

# ensure the extension is a notebook extension
if not notebookUrl.endswith('.ipynb'):
if not notebookUrl.endswith(".ipynb"):
return "URL does not point to a Jupyter Notebook", 400

# ensure the domain is in the whitelist
if not re.search(DOMAIN_WHITELIST_REGEX, notebookUrl):
return "We cannot convert a notebook from the provided domain", 400

githubUrl = ''
githubUrl = ""

# if we're including a github.com URL, we'll provide some additional links to the original github repo
if notebookUrl.startswith('https://github.com'):
if notebookUrl.startswith("https://github.com"):
# make sure we are only rendering notebooks from the nasa organization
if not notebookUrl.startswith('https://github.com/nasa/'):
if not notebookUrl.startswith("https://github.com/nasa/"):
return "Invalid notebook URL, must be in the NASA organization", 400

githubUrl = notebookUrl
notebookUrl = notebookUrl.replace(
"https://github.com", "https://raw.githubusercontent.com").replace("/blob/", "/")
"https://github.com", "https://raw.githubusercontent.com"
).replace("/blob/", "/")

# read the notebook content in
response = urlopen(notebookUrl).read().decode()

# convert it to HTML
notebook = nbformat.reads(response, as_version=4)
(body, resources) = html_exporter.from_notebook_node(notebook, resources={
'notebookUrl': notebookUrl,
'githubUrl': githubUrl,
})
(body, _resources) = html_exporter.from_notebook_node(
notebook,
resources={
"notebookUrl": notebookUrl,
"githubUrl": githubUrl,
},
)

return body
4 changes: 2 additions & 2 deletions packages/notebook-viewer/lib/html_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# a custom template for NBConvert to support iframe embedding
dl = DictLoader(
{
'custom-template': """
"custom-template": """
{%- extends 'lab/index.html.j2' -%}
{% block extra_css %}
Expand Down Expand Up @@ -41,4 +41,4 @@
)

# create a HTML exporter using our custom template
html_exporter = HTMLExporter(extra_loaders=[dl], template_file='custom-template')
html_exporter = HTMLExporter(extra_loaders=[dl], template_file="custom-template")
10 changes: 5 additions & 5 deletions packages/notebook-viewer/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Flask==2.1.2
Jinja2==3.1.2
nbconvert==7.2.7
nbformat==5.7.1
Werkzeug==2.3.7
Flask==3.1.0
Jinja2==3.1.4
nbconvert==7.16.4
nbformat==5.10.4
gunicorn==23.0.0

0 comments on commit 31caa02

Please sign in to comment.