Read a notebook from a file name or a file object :param fp: a file name or a file object :param as_version: see nbformat.read :param fmt: (optional) the jupytext format like `md`, `py:percent`, ... :param config: (optional) a Jupytext configuration object :param kwargs: (not us
(fp, as_version=nbformat.NO_CONVERT, fmt=None, config=None, **kwargs)
| 392 | |
| 393 | |
| 394 | def read(fp, as_version=nbformat.NO_CONVERT, fmt=None, config=None, **kwargs): |
| 395 | """Read a notebook from a file name or a file object |
| 396 | |
| 397 | :param fp: a file name or a file object |
| 398 | :param as_version: see nbformat.read |
| 399 | :param fmt: (optional) the jupytext format like `md`, `py:percent`, ... |
| 400 | :param config: (optional) a Jupytext configuration object |
| 401 | :param kwargs: (not used) additional parameters for nbformat.read |
| 402 | :return: the notebook |
| 403 | """ |
| 404 | if as_version != nbformat.NO_CONVERT and not isinstance(as_version, int): |
| 405 | raise TypeError("Second argument 'as_version' should be either nbformat.NO_CONVERT, or an integer.") |
| 406 | |
| 407 | if fp == "-": |
| 408 | text = sys.stdin.read() |
| 409 | # Update the input format by reference if missing |
| 410 | if isinstance(fmt, dict) and not fmt: |
| 411 | fmt.update(long_form_one_format(divine_format(text))) |
| 412 | return reads(text, fmt) |
| 413 | |
| 414 | if not hasattr(fp, "read"): |
| 415 | # Treat fp as a file name |
| 416 | fp = str(fp) |
| 417 | _, ext = os.path.splitext(fp) |
| 418 | fmt = copy(fmt or {}) |
| 419 | if not isinstance(fmt, dict): |
| 420 | fmt = long_form_one_format(fmt) |
| 421 | fmt.update({"extension": ext}) |
| 422 | with open(fp, encoding="utf-8") as stream: |
| 423 | return read(stream, as_version=as_version, fmt=fmt, config=config, **kwargs) |
| 424 | |
| 425 | if fmt is not None: |
| 426 | fmt = long_form_one_format(fmt) |
| 427 | if fmt["extension"] == ".ipynb": |
| 428 | notebook = nbformat.read(fp, as_version, **kwargs) |
| 429 | rearrange_jupytext_metadata(notebook.metadata) |
| 430 | return notebook |
| 431 | |
| 432 | return reads(fp.read(), fmt, config=config, **kwargs) |
| 433 | |
| 434 | |
| 435 | def writes(notebook, fmt, version=nbformat.NO_CONVERT, config=None, **kwargs): |
searching dependent graphs…