Skip to content

adding shell and css fence directives to midgyยค

some markdown code fences in midgy have transpile code with functions that operate on the contents of the fence. for example, toml, json, and yaml fences will load the string into python object. this post talks about the subprocess fences.

all of these behaviors can be turned off using ~~~ fences

    import midgy, pandas, json
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[82], line 1
----> 1 import midgy, pandas, jsons

ModuleNotFoundError: No module named 'jsons'
%% 

this example shows how a `toml` code fence can reused as variable

    toml_data=\
```toml
description="passed throught toml.loads"
```
    display(toml_data)
{'description': 'passed throught toml.loads'}

IPython exposes ! syntactic sugar to process shell commands. we use this code fence syntax to trigger shell commands.

%%
whats the date?

    print\
```!
date
```

see how this is similar to the `IPython` version following?

    !date

whats the date?

print\

! date see how this is similar to the IPython version following? !date

Mon Feb 26 01:13:49 PM PST 2024

Mon Feb 26 01:13:49 PM PST 2024
`````````````````````````````````````````````python %% the advantange though is that with this version we run blocks of bash print\! echo "one fish" echo "two fish" ```

which is strikingly similar the IPython %%bash magic

%%bash
echo "red fish"
echo "blue fish"

the outputs can be stored as variables in both these forms.

the advantange though is that with this version we run blocks of bash

    print\
```!
echo "one fish"
echo "two fish"
```

which is strikingly similar the `IPython` `%%bash` magic

    %%bash
    echo "red fish"
    echo "blue fish"

the outputs can be stored as variables in both these forms.



<div class="output_subarea output_stream output_stdout output_text">
<pre>one fish
two fish

red fish
blue fish
</pre>
</div>

## more examples



````````````````````````````````````````````````python
%%
maybe we want to work with out git history as a pandas series?

log = pandas.Series(
```!
git log
```
        .splitlines())
    log.groupby(log.str.extract("commit\s+(?P<commit>\S+)").ffill().commit).apply(
        lambda s: "".join(s.iloc[3:])
    ).to_frame("msg")

maybe we want to work with out git history as a pandas series?

log = pandas.Series( ```! git log

        .splitlines())
    log.groupby(log.str.extract("commit\s+(?P<commit>\S+)").ffill().commit).apply(
        lambda s: "".join(s.iloc[3:])
    ).to_frame("msg")





````````````````````````````````````````````````python
%% 
maybe we want to search some json using `jq` and explore it interactively

    display({"application/json":
            json.loads(
```!
jq  .cells[0] Untitled10.ipynb

)}, raw=True)

````````````````````````````````````````````````

maybe we want to search some json using jq and explore it interactively

display({"application/json":
        json.loads(

! jq .cells[0] Untitled10.ipynb

)}, raw=True)