julien-c HF staff commited on
Commit
df87638
1 Parent(s): 136ae30

Finish all edge cases...

Browse files
Files changed (1) hide show
  1. app.py +27 -1
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import os
2
  import httpx
 
 
3
  from nbconvert import HTMLExporter
4
  from starlette.applications import Starlette
5
  from starlette.exceptions import HTTPException
@@ -29,7 +31,31 @@ async def convert(req: Request):
29
  follow_redirects=True,
30
  # httpx no follow redirect by default
31
  )
32
- print(r.text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  return HTMLResponse("<strong>FOO</strong>")
34
 
35
 
 
1
  import os
2
  import httpx
3
+ import nbformat
4
+ from nbformat import NotebookNode, ValidationError
5
  from nbconvert import HTMLExporter
6
  from starlette.applications import Starlette
7
  from starlette.exceptions import HTTPException
 
31
  follow_redirects=True,
32
  # httpx no follow redirect by default
33
  )
34
+ if r.status_code != 200:
35
+ raise HTTPException(
36
+ 400, f"Got an error {r.status_code} when fetching remote file"
37
+ )
38
+
39
+ # Capture potential validation error:
40
+ try:
41
+ notebook_node: NotebookNode = nbformat.reads(
42
+ r.text,
43
+ as_version=4,
44
+ )
45
+ except nbformat.reader.NotJSONError:
46
+ print(400, f"Notebook is not JSON ({url})")
47
+ raise HTTPException(400, f"Notebook is not JSON ({url})")
48
+ except ValidationError as e:
49
+ print(
50
+ 400,
51
+ f"Notebook is invalid according to nbformat: {e} ({url})",
52
+ )
53
+ raise HTTPException(
54
+ 400,
55
+ f"Notebook is invalid according to nbformat: {e} ({url})",
56
+ )
57
+
58
+ print(f"Input: nbformat v{notebook_node.nbformat}.{notebook_node.nbformat_minor}")
59
  return HTMLResponse("<strong>FOO</strong>")
60
 
61