chrisjsewell / ipypublish
1 3
import shutil
2 3
import tempfile
3 3
import os
4

5 3
from ipypublish.convert.config_manager import iter_all_export_paths
6 3
from ipypublish.convert.main import IpyPubMain
7 3
from ipypublish.tests import TEST_FILES_DIR
8

9

10 3
def convert_all(inpath, outpath):
11
    """ convert notebook using all available plugins """
12 0
    for plugin_name, plugin_path in iter_all_export_paths():
13

14 0
        out_folder = tempfile.mkdtemp()
15 0
        publish = IpyPubMain(
16
            config={"IpyPubMain": {"conversion": plugin_name, "outpath": out_folder}}
17
        )
18 0
        try:
19 0
            outdata = publish(str(inpath))
20

21 0
            exporter = outdata["exporter"]
22 0
            outpath = outdata["outpath"]
23

24 0
            extension = exporter.file_extension
25 0
            out_name = os.path.splitext(os.path.basename(str(inpath)))[0] + extension
26 0
            outfile = os.path.join(out_folder, out_name)
27

28 0
            if not os.path.exists(outfile):
29 0
                raise IOError("could not find: {} for {}".format(outfile, plugin_name))
30

31 0
            shutil.copyfile(
32
                outfile, os.path.join(str(outpath), plugin_name + extension)
33
            )
34

35
        finally:
36 0
            shutil.rmtree(out_folder)
37

38

39 3
if __name__ == "__main__":
40

41 0
    _inpath = os.path.join(TEST_FILES_DIR, "ipynb1.ipynb")
42 0
    _outpath = os.path.join(TEST_FILES_DIR, "ipynb1_converted")
43 0
    convert_all(_inpath, _outpath)

Read our documentation on viewing source code .

Loading