Customizing output

Pweave has several output formats and you can customize the output with chunk options. However you may want to customize the output for different purposes.

The simplest form of customization is to update the format dictionary of an existing format. It sets chunk delimiters, output extension and figure format and width.

You can do this easily with Pweb class. Below is a small demonstration using ReST Pweave document ma.mdw.

Let’s start by creating an instance of Pweb class with markdown document:

from pweave import *
from pprint import pprint
doc = Pweb('ma.mdw', doctype = "pandoc")
Traceback (most recent call last)~/anaconda3/lib/python3.6/site-
packages/pweave/ in read_file_or_url(source)
     15     try:
---> 16         codefile =, 'r', encoding='utf-8')
     17         contents =
FileNotFoundError: [Errno 2] No such file or directory: 'ma.mdw'
During handling of the above exception, another exception occurred:
ValueError                                Traceback (most recent call
last)<ipython-input-1-e0491e5b6172> in <module>()
----> 1 doc = Pweb('ma.mdw', doctype = "pandoc")
~/anaconda3/lib/python3.6/site-packages/pweave/ in
__init__(self, source, doctype, informat, kernel, output, figdir,
     69         self.setformat(doctype)
---> 70 = informat)
     72     def _setwd(self):
~/anaconda3/lib/python3.6/site-packages/pweave/ in read(self,
string, basename, reader)
    107         if string is None:
--> 108             self.reader = Reader(file=self.source)
    109         else:
    110             self.reader = self.Reader(string=string)
~/anaconda3/lib/python3.6/site-packages/pweave/ in
__init__(self, file, string)
     36         # Get input from string or
     37         if file is not None:
---> 38             self.rawtext = read_file_or_url(self.source)
     39         else:
     40             self.rawtext = string
~/anaconda3/lib/python3.6/site-packages/pweave/ in
     18         codefile.close()
     19     except IOError:
---> 20         r = request.urlopen(source)
     21         contents ="utf-8")
     22         r.close()
~/anaconda3/lib/python3.6/urllib/ in urlopen(url, data,
timeout, cafile, capath, cadefault, context)
    221     else:
    222         opener = _opener
--> 223     return, data, timeout)
    225 def install_opener(opener):
~/anaconda3/lib/python3.6/urllib/ in open(self, fullurl,
data, timeout)
    509         # accept a URL or a Request object
    510         if isinstance(fullurl, str):
--> 511             req = Request(fullurl, data)
    512         else:
    513             req = fullurl
~/anaconda3/lib/python3.6/urllib/ in __init__(self, url,
data, headers, origin_req_host, unverifiable, method)
    327                  origin_req_host=None, unverifiable=False,
    328                  method=None):
--> 329         self.full_url = url
    330         self.headers = {}
    331         self.unredirected_hdrs = {}
~/anaconda3/lib/python3.6/urllib/ in full_url(self, url)
    353         self._full_url = unwrap(url)
    354         self._full_url, self.fragment =
--> 355         self._parse()
    357     @full_url.deleter
~/anaconda3/lib/python3.6/urllib/ in _parse(self)
    382         self.type, rest = splittype(self._full_url)
    383         if self.type is None:
--> 384             raise ValueError("unknown url type: %r" %
    385, self.selector = splithost(rest)
    386         if
ValueError: unknown url type: 'ma.mdw'

Have a look at what the format dictionary contains:

Traceback (most recent call last)<ipython-input-1-0146554762a5> in
----> 1 pprint(doc.getformat())
NameError: name 'doc' is not defined

The names of the dictionary elements are hopefully self explanatory. You’ll notice that you can specify start and end tag for code, results and term as well as block indent.

You can change the formats using Pweb.updateformat() method. Let’s set the default figure width to 10cm and figure format to pdf and figfmt specifies what format is used in the output.

doc.updateformat({'width' : '10cm', 'figfmt' : '.pdf'})
Traceback (most recent call last)<ipython-input-1-e59441017877> in
----> 1 doc.updateformat({'width' : '10cm', 'figfmt' : '.pdf'})
NameError: name 'doc' is not defined

And after setting options weave and tangle the document:

Traceback (most recent call last)<ipython-input-1-18a81aa32e9d> in
----> 1 doc.weave()
NameError: name 'doc' is not defined
Traceback (most recent call last)<ipython-input-1-2e870fa03a3a> in
----> 1 doc.tangle()
NameError: name 'doc' is not defined

View this page as Pweave document .