本文整理匯總了Python中nbconvert.RSTExporter方法的典型用法代碼示例。如果您正苦於以下問題:Python nbconvert.RSTExporter方法的具體用法?Python nbconvert.RSTExporter怎麽用?Python nbconvert.RSTExporter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類nbconvert
的用法示例。
在下文中一共展示了nbconvert.RSTExporter方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: convert
# 需要導入模塊: import nbconvert [as 別名]
# 或者: from nbconvert import RSTExporter [as 別名]
def convert(nbfile):
basename, _ = os.path.splitext(nbfile)
meta = {'metadata': {'path': '.'}}
with open(nbfile, 'r', encoding='utf-8') as nbf:
nbdata = nbformat.read(nbf, as_version=4, encoding='utf-8')
runner = ExecutePreprocessor(timeout=600, kernel_name='probscale')
runner.preprocess(nbdata, meta)
img_folder = basename + '_files'
body_raw, images = RSTExporter().from_notebook_node(nbdata)
body_final = body_raw.replace('.. image:: ', '.. image:: {}/'.format(img_folder))
with open(basename + '.rst', 'w', encoding='utf-8') as rst_out:
rst_out.write(body_final)
for img_name, img_data in images['outputs'].items():
img_path = os.path.join(img_folder, img_name)
with open(img_path, 'wb') as img:
img.write(img_data)
示例2: run
# 需要導入模塊: import nbconvert [as 別名]
# 或者: from nbconvert import RSTExporter [as 別名]
def run(self):
import nbconvert
from os.path import join
exporter = nbconvert.RSTExporter()
writer = nbconvert.writers.FilesWriter()
files = [
join("examples", "01_simple_usage.ipynb"),
join("examples", "02_advanced_usage.ipynb"),
join("examples", "03_preserving_global_structure.ipynb"),
join("examples", "04_large_data_sets.ipynb"),
]
target_dir = join("docs", "source", "examples")
for fname in files:
self.announce(f"Converting {fname}...")
directory, nb_name = fname.split("/")
nb_name, _ = nb_name.split(".")
body, resources = exporter.from_file(fname)
writer.build_directory = join(target_dir, nb_name)
writer.write(body, resources, nb_name)
示例3: parse_notebook_index
# 需要導入模塊: import nbconvert [as 別名]
# 或者: from nbconvert import RSTExporter [as 別名]
def parse_notebook_index(ntbkpth):
"""
Parse the top-level notebook index file at `ntbkpth`. Returns a list of
subdirectories in order of appearance in the index file, and a dict
mapping subdirectory name to a description.
"""
# Convert notebook to RST text in string
rex = RSTExporter()
rsttxt = rex.from_filename(ntbkpth)[0]
# Clean up trailing whitespace
rsttxt = re.sub(r'\n ', r'', rsttxt, re.M | re.S)
pthidx = {}
pthlst = []
lines = rsttxt.split('\n')
for l in lines:
m = re.match(r'^-\s+`([^<]+)\s+<([^>]+).ipynb>`__', l)
if m:
# List of subdirectories in order of appearance in index.rst
pthlst.append(m.group(2))
# Dict mapping subdirectory name to description
pthidx[m.group(2)] = m.group(1)
return pthlst, pthidx
示例4: notebook_object_to_rst
# 需要導入模塊: import nbconvert [as 別名]
# 或者: from nbconvert import RSTExporter [as 別名]
def notebook_object_to_rst(ntbk, rpth, rdir, cr=None):
"""
Convert notebook object `ntbk` to rst document at `rpth`, in directory
`rdir`. Parameter `cr` is a CrossReferenceLookup object.
"""
# Pre-process notebook prior to conversion to rst
if cr is not None:
preprocess_notebook(ntbk, cr)
# Convert notebook to rst
rex = RSTExporter()
rsttxt, rstres = rex.from_notebook_node(ntbk)
# Replace `` with ` in sphinx cross-references
rsttxt = re.sub(r':([^:]+):``(.*?)``', r':\1:`\2`', rsttxt)
# Insert a cross-reference target at top of file
reflbl = '.. _example_' + os.path.basename(rdir) + '_' + \
rpth.replace('-', '_') + ':\n'
rsttxt = reflbl + rsttxt
# Write the converted rst to disk
write_notebook_rst(rsttxt, rstres, rpth, rdir)
示例5: parse_notebook_index
# 需要導入模塊: import nbconvert [as 別名]
# 或者: from nbconvert import RSTExporter [as 別名]
def parse_notebook_index(ntbkpth):
"""
Parse the top-level notebook index file at `ntbkpth`. Returns a
list of subdirectories in order of appearance in the index file,
and a dict mapping subdirectory name to a description.
"""
# Convert notebook to RST text in string
rex = RSTExporter()
rsttxt = rex.from_filename(ntbkpth)[0]
# Clean up trailing whitespace
rsttxt = re.sub(r'\n ', r'', rsttxt, re.M | re.S)
pthidx = {}
pthlst = []
lines = rsttxt.split('\n')
for l in lines:
m = re.match(r'^-\s+`([^<]+)\s+<([^>]+).ipynb>`__', l)
if m:
# List of subdirectories in order of appearance in index.rst
pthlst.append(m.group(2))
# Dict mapping subdirectory name to description
pthidx[m.group(2)] = m.group(1)
return pthlst, pthidx
示例6: notebook_object_to_rst
# 需要導入模塊: import nbconvert [as 別名]
# 或者: from nbconvert import RSTExporter [as 別名]
def notebook_object_to_rst(ntbk, rpth, cr=None):
"""
Convert notebook object `ntbk` to rst document at `rpth`, in
directory `rdir`. Parameter `cr` is a CrossReferenceLookup
object.
"""
# Parent directory of file rpth
rdir = os.path.dirname(rpth)
# File basename
rb = os.path.basename(os.path.splitext(rpth)[0])
# Pre-process notebook prior to conversion to rst
if cr is not None:
preprocess_notebook(ntbk, cr)
# Convert notebook to rst
rex = RSTExporter()
rsttxt, rstres = rex.from_notebook_node(ntbk)
# Replace `` with ` in sphinx cross-references
rsttxt = re.sub(r':([^:]+):``(.*?)``', r':\1:`\2`', rsttxt)
# Insert a cross-reference target at top of file
reflbl = '.. _examples_' + os.path.basename(rdir) + '_' + \
rb.replace('-', '_') + ':\n\n'
rsttxt = reflbl + rsttxt
# Write the converted rst to disk
write_notebook_rst(rsttxt, rstres, rb, rdir)
示例7: convert_notebook
# 需要導入模塊: import nbconvert [as 別名]
# 或者: from nbconvert import RSTExporter [as 別名]
def convert_notebook(nb: notebooknode.NotebookNode, resources: Dict[str, str]):
nb = _process_nb(nb)
writer = nbconvert.RSTExporter()
body, resources = writer.from_notebook_node(nb, resources)
body = _process_rst(body)
return body, resources