Skip to content
This repository has been archived by the owner on Jun 23, 2020. It is now read-only.

[FEATURE REQUEST]: configuration and include scripts in YAML metadata block #7

Closed
mitinarseny opened this issue Nov 6, 2019 · 2 comments

Comments

@mitinarseny
Copy link

Hello again :)

It is possible to read metadata of document within filter. It is much more convenient to define configurations for this filter in YAML metadata block within markdown document that is going to be processed. Moreover, pandoc has support for providing this information in separate file. This is inconvenient to have another file especially for this filter.

It will be convenient to add include scripts to this YAML metadata block as well.

So, the final example will look as following (with #6 implemented):

---
title: How to model Hypergeometric Distribution?
pyplot_interpreter: python3
pyplot_format: svg
pyplot_links: false
pyplot_includes:
  plotly: |
    ```
    import plotly as py
    import plotly.graph_objs as go
    ```
  hg: |
    ```
    import scipy as sp

    # HG implements hypergeometric distribution
    class HG(object):
        def __init__(self, N: int, m: int, n: int):
            self.N = N
            self.m = m
            self.n = n

        def p(self, k: int) -> float:
            return sp.special.comb(self.m, k) \
                   * sp.special.comb(self.N - self.m, self.n - k) \
                   / sp.special.comb(self.N, self.n)

        def __str__(self) -> str:
            return f'HG({self.N}, {self.m}, {self.n})'
    
    xi = HG(30, 15, 20)
    hist_data_x = np.arange(xi.n+1)
    ```
---

# Try plotly

<!-- In this code block I need only to include `mpl` 
as I do not use any things from `data`:-->
```{.pyplot includes="plotly"}
fig = go.Figure(
    data=(go.Scatter(
        x=list(range(10)),
        y=list(map(lambda x: x**3, range(10))),
        mode='markers',
    ),)
)
```

# Histogram

Here is a histogram of $HG(30,15,20)$:
```{.pyplot includes="plotly,hg"}
hg_hist_fig = go.Figure(
    data=(go.Scatter(
        x=list(hist_data_x),
        y=list(map(xi.p, hist_data_x)),
        mode='markers',
    ),),
    layout=go.Layout(
        title=go.layout.Title(
            text=r'$\xi \sim ' + str(xi) + '$',
            x=.5,
        ),
        yaxis=go.layout.YAxis(title=go.layout.yaxis.Title(
            text=r'$\mathbb{P}(\xi=k)$',
        )),
        xaxis=go.layout.XAxis(title=go.layout.xaxis.Title(
            text=r'$k$',
        )),
    ),
)
```

Also, it will be very nice if I can include scripts that were already in document and reference them by their name without executing them separately. Look at this example:

# Modelling

Let's firstly define class `HG` which would contain information about
parameters $N$, $m$ and $n$ and method `p(k)` for calculating
the probability of event when $\xi = k$:
```{.python .pyplot .noExec name="class"}
import scipy as sp

class HG(object):
    def __init__(self, N: int, m: int, n: int):
        self.N = N
        self.m = m
        self.n = n

    def p(self, k: int) -> float:
        return sp.special.comb(self.m, k) \
               * sp.special.comb(self.N - self.m, self.n - k) \
               / sp.special.comb(self.N, self.n)

    def __str__(self) -> str:
        return f'HG({self.N}, {self.m}, {self.n})'
```
Then create object of random variable $\xi \sim HG(30, 15, 20)$:
```{.python .pyplot .noExec name="xi"}
xi = HG(30, 15, 20)
```
Next step is to define interval $\overline{0, n}$ for $k$ where we will draw our histogram:
```{.python .pyplot .noExec name="data"}
import numpy as np

hist_data_x = np.arange(xi.n+1)
```
Finally, draw the plot:
```{.python .pyplot .noExec name="plot"}
import plotly
import plotly.graph_objs as go
hg_hist_fig = go.Figure(
    data=(go.Scatter(
        x=list(hist_data_x),
        y=list(map(xi.p, hist_data_x)),
        mode='markers',
    ),),
    layout=go.Layout(
        title=go.layout.Title(
            text=r'$\xi \sim ' + str(xi) + '$',
            x=.5,
        ),
        yaxis=go.layout.YAxis(title=go.layout.yaxis.Title(
            text=r'$\mathbb{P}(\xi=k)$',
        )),
        xaxis=go.layout.XAxis(title=go.layout.xaxis.Title(
            text=r'$k$',
        )),
    ),
)
```
The result would be following:
```{.pyplot include="class,xi,data,plot"}
# I do not have to copy-paste anything here.
```

It would be awesome if you can implement this!

@LaurentRDC
Copy link
Owner

Including scripts in the YAML metadata defeats the purpose of have scripts as separate files.

I use pandoc-pyplot to write book-length documents, separated into one section per file. Then, scripts can be re-used between sections because there is a single location where they are. This is also why configuration is stored in a separate YAML file; configuration is reused many times when I compile a document with Pandoc.

I understand that what you suggest would save some typing, but at the expense of much more complexity.

@mitinarseny
Copy link
Author

Then, scripts can be re-used between sections because there is a single location where they are. This is also why configuration is stored in a separate YAML file;

As I said before:

Moreover, pandoc has support for providing this information in separate file.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants