1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
# Python module Pweave
# Matti Pastell 2010-2016
# http://mpastell.com/pweave
from . import readers
from .pweb import *
from .formatters import *
from .readers import *
from .processors import *
from .config import *
__version__ = '0.30.3'
def weave(file, doctype=None, informat=None, kernel="python3", plot=True,
docmode=False, cache=False,
figdir='figures', cachedir='cache',
figformat=None, listformats=False,
output=None, mimetype=None,):
"""
Processes a Pweave document and writes output to a file
:param file: ``string`` input file
:param doctype: ``string`` output document format: call with listformats true to get list of supported formats.
:param informat: ``string`` input format: "noweb", "markdown", "notebook" or "script"
:param kernel: ``string`` Jupyter kernel used to run code: default is python3
:param plot: ``bool`` use matplotlib
:param docmode: ``bool`` use documentation mode, chunk code and results will be loaded from cache and inline code will be hidden
:param cache: ``bool`` Cache results to disk for documentation mode
:param figdir: ``string`` directory path for figures
:param cachedir: ``string`` directory path for cached results used in documentation mode
:param figformat: ``string`` format for saved figures (e.g. '.png'), if None then the default for each format is used
:param listformats: ``bool`` List available formats and exit
:param output: ``string`` output file
:param mimetype: ``string`` Source document's text mimetype. This is used to set cell
type in Jupyter notebooks.
"""
if listformats:
PwebFormats.listformats()
return
if figformat is not None:
sys.stdout.write("figformat option is not implemented for Pweave >= 0.3")
assert file is not None and file != "", "No input specified"
doc = Pweb(file, informat=informat, doctype=doctype,
kernel=kernel, output=output, figdir=figdir,
mimetype=mimetype
)
doc.documentationmode = docmode
rcParams["usematplotlib"] = plot
rcParams["cachedir"] = cachedir
rcParams["storeresults"] = cache
doc.weave()
def tangle(file, informat = None):
"""Tangles a noweb file i.e. extracts code from code chunks to a .py file
:param file: ``string`` the pweave document containing the code
"""
doc = Pweb(file, kernel = None, informat = informat)
doc.tangle()
def publish(file, doc_format="html", theme="skeleton", latex_engine="pdflatex",
output = None):
"""Publish python script and results to html or pdf, expects that doc
chunks are written in markdown.
:param file: ``string`` input file"
:param format: ``string`` output format "html" of "pdf", pdf output
requires pandoc and pdflatex in your path.
:param latex_engine: ``string`` the command for running latex. Defaults to "pdflatex".
:param output: ``string`` output file. Use .tex extension for pdf output.
"""
if doc_format == "html":
pformat = "md2html"
elif doc_format == "pdf":
pformat = "pandoc2latex"
else:
print("Unknown format, exiting")
return
doc = Pweb(file, kernel="python3", doctype=pformat,
output=output)
doc.theme = theme
doc.read()
doc.run()
doc.format()
doc.write()
if doc_format == "pdf":
try:
latex = Popen([latex_engine, doc.sink], stdin=PIPE, stdout=PIPE)
print("Running " + latex_engine + "...")
except:
print("Can't find " + latex_engine + ", no pdf produced!")
return
x = latex.communicate()[0].decode('utf-8')
print("\n".join(x.splitlines()[-2:]))
def spin(file):
"""Convert input file from script format to noweb format, similar to Knitr's spin."""
doc = readers.PwebConvert(file)
def convert(file, informat="noweb", outformat="script", pandoc_args=None,
listformats=False):
"""Convert input file from script to noweb or vice versa
:param file: ``string`` input file
:param informat: ``string`` input format noweb, script or notebook
:param outformat: ``string`` input format noweb or script
:param pandoc_args: ``string`` arguments passed to pandoc to convert doc chunks.
e.g. to convert from markdown to latex use: `"-f markdown -t latex"` .
Note that each doc chunk is converted separately so you can't use pandocs -s option.
:param listformats: ``bool`` List available formats and exit
"""
if listformats:
readers.PwebConverters.listformats()
return
Converter = readers.PwebConverters.formats[outformat]['class']
# pandoc_args = None skips the call to pandoc
doc = Converter(file, informat, outformat, pandoc_args)
doc.convert()
doc.write()
def listformats():
"""List output formats"""
PwebFormats.listformats()
|