From 9d0feb068c0822afaf606be21b3bc8ef6d174278 Mon Sep 17 00:00:00 2001 From: Butch Landingin Date: Mon, 10 Aug 2020 13:51:02 +0800 Subject: [PATCH] Remove colab vendor specific tags which cause nbdev_build_docs to fail --- docs/clean.html | 65 ++++++++++++++++++++++++++++++++++++++++------ nbdev/_nbdev.py | 2 ++ nbdev/clean.py | 18 ++++++++++--- nbs/07_clean.ipynb | 55 ++++++++++++++++++++++++++++++--------- 4 files changed, 117 insertions(+), 23 deletions(-) diff --git a/docs/clean.html b/docs/clean.html index de3bc7885..3147946ea 100644 --- a/docs/clean.html +++ b/docs/clean.html @@ -97,7 +97,39 @@

rm_execution_count -

clean_cell_output[source]

clean_cell_output(cell)

+

clean_output_data_vnd[source]

clean_output_data_vnd(o)

+
+

Remove application/vnd.google.colaboratory.intrinsic+json in data entries

+ + + + + + + + + + {% endraw %} + + {% raw %} + +
+ +
+ {% endraw %} + + {% raw %} + +
+ +
+
+ +
+ + +
+

clean_cell_output[source]

clean_cell_output(cell)

Remove execution count in cell

@@ -136,7 +168,7 @@

clean_cell_output -

clean_cell[source]

clean_cell(cell, clear_all=False)

+

clean_cell[source]

clean_cell(cell, clear_all=False)

Clean cell by removing superluous metadata or everything except the input if clear_all

@@ -160,7 +192,14 @@

clean_cell
tst = {'cell_type': 'code',
        'execution_count': 26,
        'metadata': {'hide_input': True, 'meta': 23},
-       'outputs': [{'execution_count': 2, 'output': 'super'}],
+       'outputs': [{'execution_count': 2, 
+                    'data': {
+                        'application/vnd.google.colaboratory.intrinsic+json': {
+                            'type': 'string'},
+                        'plain/text': ['sample output',]
+                    },
+                    'output': 'super'}],
+       
        'source': 'awesome_code'}
 tst1 = tst.copy()
 
@@ -168,7 +207,9 @@ 

clean_celltest_eq(tst, {'cell_type': 'code', 'execution_count': None, 'metadata': {'hide_input': True}, - 'outputs': [{'execution_count': None, 'output': 'super'}], + 'outputs': [{'execution_count': None, + 'data': {'plain/text': ['sample output',]}, + 'output': 'super'}], 'source': 'awesome_code'}) clean_cell(tst1, clear_all=True) @@ -204,7 +245,7 @@

clean_cell -

clean_nb[source]

clean_nb(nb, clear_all=False)

+

clean_nb[source]

clean_nb(nb, clear_all=False)

Clean nb from superfulous metadata, passing clear_all to clean_cell

@@ -228,7 +269,13 @@

clean_nb
tst = {'cell_type': 'code',
        'execution_count': 26,
        'metadata': {'hide_input': True, 'meta': 23},
-       'outputs': [{'execution_count': 2, 'output': 'super'}],
+       'outputs': [{'execution_count': 2,
+                    'data': {
+                        'application/vnd.google.colaboratory.intrinsic+json': {
+                            'type': 'string'},
+                        'plain/text': ['sample output',]
+                    },
+                    'output': 'super'}],
        'source': 'awesome_code'}
 nb = {'metadata': {'kernelspec': 'some_spec', 'jekyll': 'some_meta', 'meta': 37},
       'cells': [tst]}
@@ -237,7 +284,9 @@ 

clean_nbtest_eq(nb['cells'][0], {'cell_type': 'code', 'execution_count': None, 'metadata': {'hide_input': True}, - 'outputs': [{'execution_count': None, 'output': 'super'}], + 'outputs': [{'execution_count': None, + 'data': { 'plain/text': ['sample output',]}, + 'output': 'super'}], 'source': 'awesome_code'}) test_eq(nb['metadata'], {'kernelspec': 'some_spec', 'jekyll': 'some_meta'})

@@ -287,7 +336,7 @@

Main function -

nbdev_clean_nbs[source]

nbdev_clean_nbs(fname:"A notebook name or glob to convert"=None, clear_all:"Clean all metadata and outputs"=False, disp:"Print the cleaned outputs"=False, read_input_stream:"Read input stram and not nb folder"=False)

+

nbdev_clean_nbs[source]

nbdev_clean_nbs(fname:"A notebook name or glob to convert"=None, clear_all:"Clean all metadata and outputs"=False, disp:"Print the cleaned outputs"=False, read_input_stream:"Read input stram and not nb folder"=False)

Clean all notebooks in fname to avoid merge conflicts

diff --git a/nbdev/_nbdev.py b/nbdev/_nbdev.py index 93ad0be1c..6a740437e 100644 --- a/nbdev/_nbdev.py +++ b/nbdev/_nbdev.py @@ -107,6 +107,8 @@ "nbdev_install_git_hooks": "06_cli.ipynb", "nbdev_new": "06_cli.ipynb", "rm_execution_count": "07_clean.ipynb", + "clean_output_data_vnd": "07_clean.ipynb", + "colab_json": "07_clean.ipynb", "clean_cell_output": "07_clean.ipynb", "cell_metadata_keep": "07_clean.ipynb", "nb_metadata_keep": "07_clean.ipynb", diff --git a/nbdev/clean.py b/nbdev/clean.py index 0ecb1f437..695776e38 100644 --- a/nbdev/clean.py +++ b/nbdev/clean.py @@ -1,7 +1,7 @@ # AUTOGENERATED! DO NOT EDIT! File to edit: nbs/07_clean.ipynb (unless otherwise specified). -__all__ = ['rm_execution_count', 'clean_cell_output', 'cell_metadata_keep', 'nb_metadata_keep', 'clean_cell', - 'clean_nb', 'nbdev_clean_nbs'] +__all__ = ['rm_execution_count', 'clean_output_data_vnd', 'colab_json', 'clean_cell_output', 'cell_metadata_keep', + 'nb_metadata_keep', 'clean_cell', 'clean_nb', 'nbdev_clean_nbs'] # Cell import io,sys,json,glob @@ -14,11 +14,23 @@ def rm_execution_count(o): "Remove execution count in `o`" if 'execution_count' in o: o['execution_count'] = None +# Cell +colab_json = "application/vnd.google.colaboratory.intrinsic+json" +def clean_output_data_vnd(o): + "Remove `application/vnd.google.colaboratory.intrinsic+json` in data entries" + if 'data' in o: + data = o['data'] + if colab_json in data: + new_data = {k:v for k,v in data.items() if k != colab_json} + o['data'] = new_data + # Cell def clean_cell_output(cell): "Remove execution count in `cell`" if 'outputs' in cell: - for o in cell['outputs']: rm_execution_count(o) + for o in cell['outputs']: + rm_execution_count(o) + clean_output_data_vnd(o) # Cell cell_metadata_keep = ["hide_input", "tags"] diff --git a/nbs/07_clean.ipynb b/nbs/07_clean.ipynb index 5a4280cb9..93c742431 100644 --- a/nbs/07_clean.ipynb +++ b/nbs/07_clean.ipynb @@ -80,6 +80,23 @@ " if 'execution_count' in o: o['execution_count'] = None" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%nbdev_export\n", + "colab_json = \"application/vnd.google.colaboratory.intrinsic+json\"\n", + "def clean_output_data_vnd(o):\n", + " \"Remove `application/vnd.google.colaboratory.intrinsic+json` in data entries\"\n", + " if 'data' in o:\n", + " data = o['data']\n", + " if colab_json in data:\n", + " new_data = {k:v for k,v in data.items() if k != colab_json}\n", + " o['data'] = new_data" + ] + }, { "cell_type": "code", "execution_count": null, @@ -90,7 +107,9 @@ "def clean_cell_output(cell):\n", " \"Remove execution count in `cell`\"\n", " if 'outputs' in cell:\n", - " for o in cell['outputs']: rm_execution_count(o)" + " for o in cell['outputs']: \n", + " rm_execution_count(o)\n", + " clean_output_data_vnd(o)" ] }, { @@ -129,7 +148,14 @@ "tst = {'cell_type': 'code',\n", " 'execution_count': 26,\n", " 'metadata': {'hide_input': True, 'meta': 23},\n", - " 'outputs': [{'execution_count': 2, 'output': 'super'}],\n", + " 'outputs': [{'execution_count': 2, \n", + " 'data': {\n", + " 'application/vnd.google.colaboratory.intrinsic+json': {\n", + " 'type': 'string'},\n", + " 'plain/text': ['sample output',]\n", + " },\n", + " 'output': 'super'}],\n", + " \n", " 'source': 'awesome_code'}\n", "tst1 = tst.copy()\n", "\n", @@ -137,7 +163,9 @@ "test_eq(tst, {'cell_type': 'code',\n", " 'execution_count': None,\n", " 'metadata': {'hide_input': True},\n", - " 'outputs': [{'execution_count': None, 'output': 'super'}],\n", + " 'outputs': [{'execution_count': None, \n", + " 'data': {'plain/text': ['sample output',]},\n", + " 'output': 'super'}],\n", " 'source': 'awesome_code'})\n", "\n", "clean_cell(tst1, clear_all=True)\n", @@ -170,7 +198,13 @@ "tst = {'cell_type': 'code',\n", " 'execution_count': 26,\n", " 'metadata': {'hide_input': True, 'meta': 23},\n", - " 'outputs': [{'execution_count': 2, 'output': 'super'}],\n", + " 'outputs': [{'execution_count': 2,\n", + " 'data': {\n", + " 'application/vnd.google.colaboratory.intrinsic+json': {\n", + " 'type': 'string'},\n", + " 'plain/text': ['sample output',]\n", + " },\n", + " 'output': 'super'}],\n", " 'source': 'awesome_code'}\n", "nb = {'metadata': {'kernelspec': 'some_spec', 'jekyll': 'some_meta', 'meta': 37},\n", " 'cells': [tst]}\n", @@ -179,7 +213,9 @@ "test_eq(nb['cells'][0], {'cell_type': 'code',\n", " 'execution_count': None,\n", " 'metadata': {'hide_input': True},\n", - " 'outputs': [{'execution_count': None, 'output': 'super'}],\n", + " 'outputs': [{'execution_count': None, \n", + " 'data': { 'plain/text': ['sample output',]},\n", + " 'output': 'super'}],\n", " 'source': 'awesome_code'})\n", "test_eq(nb['metadata'], {'kernelspec': 'some_spec', 'jekyll': 'some_meta'})" ] @@ -282,9 +318,11 @@ "Converted 03_export2html.ipynb.\n", "Converted 04_test.ipynb.\n", "Converted 05_merge.ipynb.\n", + "Converted 05a_conda.ipynb.\n", "Converted 06_cli.ipynb.\n", "Converted 07_clean.ipynb.\n", "Converted 08_flag_tests.ipynb.\n", + "Converted 09_nbdev_callback_test.ipynb.\n", "Converted 99_search.ipynb.\n", "Converted index.ipynb.\n", "Converted tutorial.ipynb.\n" @@ -295,13 +333,6 @@ "%nbdev_hide\n", "notebook2script()" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": {