{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "510ed03d-0c3b-4caf-910d-e6eaec08fd63", "metadata": {}, "outputs": [], "source": [ "#|default_exp qmd" ] }, { "cell_type": "markdown", "id": "f0f931c2", "metadata": {}, "source": [ "# qmd\n", "\n", "> Basic qmd generation helpers (experimental)" ] }, { "cell_type": "code", "execution_count": null, "id": "6a35c7c4-748f-4c82-a9bf-c780a8d83e90", "metadata": {}, "outputs": [], "source": [ "#|export\n", "from __future__ import annotations\n", "import sys,os,inspect\n", "\n", "from fastcore.utils import *\n", "from fastcore.meta import delegates" ] }, { "cell_type": "code", "execution_count": null, "id": "1e623a3d-3e77-44c6-adf3-4768b78328c5", "metadata": {}, "outputs": [], "source": [ "#|hide\n", "from fastcore.test import *" ] }, { "cell_type": "code", "execution_count": null, "id": "afe1a210", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def setup():\n", " mod = inspect.getmodule(inspect.currentframe().f_back)\n", " path = Path(mod.__file__)\n", " os.chdir(path.parent)\n", " sys.stdout = open(path.with_suffix('.qmd'), 'w')\n", " print(mod.__doc__)" ] }, { "cell_type": "code", "execution_count": null, "id": "5a64f1f4", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def meta(md, # Markdown to add meta to\n", " classes=None, # List of CSS classes to add\n", " style=None, # Dict of CSS styles to add\n", " **kwargs): # Additional attributes to add to meta\n", " \"A metadata section for qmd div in `{}`\"\n", " if style: kwargs['style'] = \"; \".join(f'{k}: {v}' for k,v in style.items())\n", " props = ' '.join(f'{k}=\"{v}\"' for k,v in kwargs.items())\n", " classes = ' '.join('.'+c for c in L(classes))\n", " meta = []\n", " if classes: meta.append(classes)\n", " if props: meta.append(props)\n", " meta = ' '.join(meta)\n", " return md + (\"{\" + meta + \"}\" if meta else \"\")" ] }, { "cell_type": "code", "execution_count": null, "id": "52637a70", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def div(txt, # Markdown to add meta to\n", " classes=None, # List of CSS classes to add\n", " style=None, # Dict of CSS styles to add\n", " **kwargs):\n", " \"A qmd div with optional metadata section\"\n", " return meta(\"::: \", classes=classes, style=style, **kwargs) + f\"\\n\\n{txt}\\n\\n:::\\n\\n\"" ] }, { "cell_type": "code", "execution_count": null, "id": "f9f499f4", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def img(fname, # Image to link to\n", " classes=None, # List of CSS classes to add\n", " style=None, # Dict of CSS styles to add\n", " height=None, # Height attribute\n", " relative=None, # Tuple of (position,px)\n", " link=False, # Hyperlink to this image\n", " **kwargs):\n", " \"A qmd image\"\n", " kwargs,style = kwargs or {}, style or {}\n", " if height: kwargs[\"height\"]= f\"{height}px\"\n", " if relative:\n", " pos,px = relative\n", " style[\"position\"] = \"relative\"\n", " style[pos] = f\"{px}px\"\n", " res = meta(f'![]({fname})', classes=classes, style=style, **kwargs)\n", " return f'[{res}]({fname})' if link else res" ] }, { "cell_type": "code", "execution_count": null, "id": "16d7aa5f", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def btn(txt, # Button text\n", " link, # Button link URL\n", " classes=None, # List of CSS classes to add\n", " style=None, # Dict of CSS styles to add\n", " **kwargs):\n", " \"A qmd button\"\n", " return meta(f'[{txt}]({link})', classes=classes, style=style, role=\"button\")" ] }, { "cell_type": "markdown", "id": "aa35b010", "metadata": {}, "source": [ "## Export -" ] }, { "cell_type": "code", "execution_count": null, "id": "3d8031ce", "metadata": {}, "outputs": [], "source": [ "#|hide\n", "import nbdev; nbdev.nbdev_export()" ] }, { "cell_type": "code", "execution_count": null, "id": "12588a26-43a6-42c4-bacd-896293c871ab", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }