Coverage for /home/tbone/.local/share/hatch/env/virtual/importnb-aVRh-lqt/test-legacy.interactive/lib/python3.9/site-packages/importnb/utils/export.py: 0%

1# coding: utf-8

2"""# The `export` module

3

4...provides compatibility for Python and IPython through [`compile_python`](compile_python.ipynb) and [`compile_ipython`](compile_ipython.ipynb), respectively.

5

6 >>> from importnb.utils.export import export

7"""

8

9 from json import loads

10 from pathlib import Path

11

12 from ..loader import dedent

13

14 try:

15 from black import format_str

16 except ImportError:

17 format_str = lambda x, i: x

18

19

20 def block_str(str):

21 quotes = '"""'

22 if quotes in str:

23 quotes = "'''"

24 return "{quotes}{str}\n{quotes}\n".format(quotes=quotes, str=str)

25

26

27"""The export function

28"""

29

30

31 def export(file, to=None):

32 code = """# coding: utf-8"""

33 with open(str(file), "r") as f:

34 for cell in loads(f.read())["cells"]:

35 if cell["cell_type"] == "markdown":

36 code += "\n" + block_str("".join(cell["source"]))

37 elif cell["cell_type"] == "code":

38 code += "\n" + dedent("".join(cell["source"]))

39 to and Path(to).with_suffix(".py").write_text(format_str(code, 100))

40 return code