from prestring.python import Module from dictknife import DictWalker from dictknife import loading w = DictWalker(["lines"]) d = loading.loadfile(format="json") r = [] for _, d in w.walk(d): if d["language"] == "python" or d["language"] == "py": r.append(d["lines"]) m = Module() m.from_("nbreversible", "code") for lines in r: with m.with_("code()"): for line in lines: if line.startswith("%"): m.stmt("#{}", line) else: m.stmt(line.rstrip()) m.sep() print(m)
from prestring.python import Module import matplotlib.cm as cm m = Module() # noqa m.from_('nbreversible', 'code') m.import_('pandas as pd') m.import_('numpy as np') m.import_('seaborn as sns') m.sep() m.stmt('"# [jupyter][matplotlib][python] color mapの一覧をheatmapで"') m.stmt('# %matplotlib inline') with m.with_('code()'): m.stmt('xs = np.arange(1, 10)') m.stmt('ys = np.arange(1, 10).reshape(9, 1)') m.stmt('m = xs * ys') m.stmt('df = pd.DataFrame(m)') m.stmt('df') for name in cm.cmap_d.keys(): m.stmt(f'"## {name}"') m.sep() with m.with_("code()"): m.stmt(f"sns.heatmap(df, {name!r})") m.sep() print(m)