Reading json and filtering by nested value of tag

Hey!

I want to build a theme that can be used for scientific papers and reproducible research. The idea is to have articles, papers or posts going over a methodology. The code lives in a jupyter notebook. A notebook is actually a json file with different cells.

I find that when writing scientific blog posts explaining a data analysis, if there is a lot of code, it might be best to just import specific cells from the notebook. If the code changes in the notebook, then a hugo build is triggered to update the paper/page.

I am ok renaming the notebook to a .json and putting it under the data directory.

Here is what the json looks like (you need to scroll to see it all)

    "cells": [
        {
            "cell_type": "code",
            "execution_count": 11,
            "metadata": {},
            "outputs": [],
            "source": [
                "import numpy as np\n",
                "from matplotlib import pyplot as plt\n",
                "import seaborn as sns\n",
                "from IPython.display import display, HTML \n",
                "import imgkit"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 12,
            "metadata": {},
            "outputs": [],
            "source": [
                "sns.set(style=\"darkgrid\")\n",
                "# current_pallette = sns.color_palette(\"muted\")\n",
                "\n",
                "sns.set_palette(sns.hls_palette(8, l = 0.8, s = 0.8))\n",
                "sns.set_color_codes()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 13,
            "metadata": {
                "slideshow": {
                    "slide_type": "fragment"
                },
                "tags": [
                    "section1a"
                ]
            },
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "0.14354692507258635"
                        ]
                    },
                    "execution_count": 13,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "# Daily infection rate\n",
                "\n",
                "def beta(L, R0):\n",
                "    gamma = R0**(1/L)\n",
                "    gamma_sum = np.array([gamma**(L-s) for s in range(1,L+1)]).sum()\n",
                "    return R0 / gamma_sum\n",
                "\n",
                "beta(10, 2.0)"
            ]
        }
    ]
}

I want to get to cell where there is the tag = “section1a”. from that cell I want to add the “source” to the generated html. In this case, it is the daily infection rate.

I did a piece of code that extracts the source for me in python but I am looking for a way to do this organically in Hugo, so I can make this an open source theme.

I would appreciate some help.

Here is my own draft solution.

{{ range $.Site.Data.notebooks.testnotebook.cells }}
    {{ $cells := . }}
    {{ range $cells }}
        {{ if $cells.metadata }}
                {{ if in $cells.metadata.tags  "section1a" }}
                   {{ range  $cells.source }} 
                        {{ markdownify . }}
                   {{ end }}
                {{ end }}
        {{ end }}
    {{ end }}
{{ end }}

I am going to be looking how to use a shortcode inside a shortcode, so I can call the code highlight shortcode :slight_smile:

this code works

{{ $notebook_name :=   .Get "src"  }}
{{ $cell_tag :=   .Get "tag" }}

 
{{ range $.Site.Data.notebooks.testnotebook.cells  }}
    {{ $cells := . }}
    {{ range $cells.source }}
        {{ if $cells.metadata }}
                {{ if in $cells.metadata.tags  $cell_tag }}
                    {{ markdownify . }}
                {{ end }}
        {{ end }}
    {{ end }}
{{ end }}

Now i need to figure out how to replace the path for range

range $.Site.Data.notebooks.testnotebook.cells

The value is hardcoded to notebooks.testnotebook.cells, i need to replace notebooks.testnotebook with $notebook_name, any hints?