|
184 | 184 | "outputs": [], |
185 | 185 | "source": [ |
186 | 186 | "#|export\n", |
187 | | - "def dict2nb(js):\n", |
| 187 | + "def dict2nb(js=None, **kwargs):\n", |
188 | 188 | " \"Convert dict `js` to an `AttrDict`, \"\n", |
189 | | - " nb = dict2obj(js)\n", |
| 189 | + " nb = dict2obj(js or kwargs)\n", |
190 | 190 | " nb.cells = nb.cells.enumerate().starmap(NbCell)\n", |
191 | 191 | " return nb" |
192 | 192 | ] |
|
250 | 250 | { |
251 | 251 | "data": { |
252 | 252 | "text/plain": [ |
253 | | - "([<ast.Expr at 0x107d476a0>], <ast.Add at 0x105235ee0>)" |
| 253 | + "([<ast.Expr at 0x1067c36a0>], <ast.Add at 0x1039bb2e0>)" |
254 | 254 | ] |
255 | 255 | }, |
256 | 256 | "execution_count": null, |
|
331 | 331 | "minimal.path_" |
332 | 332 | ] |
333 | 333 | }, |
| 334 | + { |
| 335 | + "cell_type": "markdown", |
| 336 | + "metadata": {}, |
| 337 | + "source": [ |
| 338 | + "# Creating a notebook" |
| 339 | + ] |
| 340 | + }, |
| 341 | + { |
| 342 | + "cell_type": "code", |
| 343 | + "execution_count": null, |
| 344 | + "metadata": {}, |
| 345 | + "outputs": [], |
| 346 | + "source": [ |
| 347 | + "#|export\n", |
| 348 | + "def new_nb(cells=None, meta=None, nbformat=4, nbformat_minor=5):\n", |
| 349 | + " \"Returns an empty new notebook\"\n", |
| 350 | + " return dict2nb(cells=cells or [],metadata=meta or {},nbformat=nbformat,nbformat_minor=nbformat_minor)" |
| 351 | + ] |
| 352 | + }, |
| 353 | + { |
| 354 | + "cell_type": "markdown", |
| 355 | + "metadata": {}, |
| 356 | + "source": [ |
| 357 | + "Use this function when creating a new notebook. Useful for when you don't want to create a notebook on disk first and then read it." |
| 358 | + ] |
| 359 | + }, |
| 360 | + { |
| 361 | + "cell_type": "code", |
| 362 | + "execution_count": null, |
| 363 | + "metadata": {}, |
| 364 | + "outputs": [], |
| 365 | + "source": [ |
| 366 | + "#| export\n", |
| 367 | + "def mk_cell(text, # `source` attr in cell\n", |
| 368 | + " cell_type='code', # `cell_type` attr in cell\n", |
| 369 | + " **kwargs): # any other attrs to add to cell\n", |
| 370 | + " \"Create an `NbCell` containing `text`\"\n", |
| 371 | + " assert cell_type in {'code', 'markdown', 'raw'}\n", |
| 372 | + " if 'metadata' not in kwargs: kwargs['metadata']={}\n", |
| 373 | + " return NbCell(0, dict(cell_type=cell_type, source=text, directives_={}, **kwargs))" |
| 374 | + ] |
| 375 | + }, |
| 376 | + { |
| 377 | + "cell_type": "code", |
| 378 | + "execution_count": null, |
| 379 | + "metadata": {}, |
| 380 | + "outputs": [ |
| 381 | + { |
| 382 | + "data": { |
| 383 | + "text/markdown": [ |
| 384 | + "```json\n", |
| 385 | + "{ 'cell_type': 'code',\n", |
| 386 | + " 'directives_': {},\n", |
| 387 | + " 'execution_count': 0,\n", |
| 388 | + " 'idx_': 0,\n", |
| 389 | + " 'metadata': {},\n", |
| 390 | + " 'source': 'print(1)'}\n", |
| 391 | + "```" |
| 392 | + ], |
| 393 | + "text/plain": [ |
| 394 | + "{'cell_type': 'code',\n", |
| 395 | + " 'source': 'print(1)',\n", |
| 396 | + " 'directives_': {},\n", |
| 397 | + " 'execution_count': 0,\n", |
| 398 | + " 'metadata': {},\n", |
| 399 | + " 'idx_': 0}" |
| 400 | + ] |
| 401 | + }, |
| 402 | + "execution_count": null, |
| 403 | + "metadata": {}, |
| 404 | + "output_type": "execute_result" |
| 405 | + } |
| 406 | + ], |
| 407 | + "source": [ |
| 408 | + "mk_cell('print(1)', execution_count=0)" |
| 409 | + ] |
| 410 | + }, |
334 | 411 | { |
335 | 412 | "cell_type": "markdown", |
336 | 413 | "metadata": {}, |
|
347 | 424 | "#|export\n", |
348 | 425 | "def nb2dict(d, k=None):\n", |
349 | 426 | " \"Convert parsed notebook to `dict`\"\n", |
350 | | - " if k in ('source',): return d.splitlines(keepends=True)\n", |
| 427 | + " if k=='source': return d.splitlines(keepends=True)\n", |
351 | 428 | " if isinstance(d, (L,list)): return list(L(d).map(nb2dict))\n", |
352 | 429 | " if not isinstance(d, dict): return d\n", |
353 | 430 | " return dict(**{k:nb2dict(v,k) for k,v in d.items() if k[-1] != '_'})" |
|
450 | 527 | "cell_type": "markdown", |
451 | 528 | "metadata": {}, |
452 | 529 | "source": [ |
453 | | - "# Creating a notebook" |
| 530 | + "Here's how to put all the pieces of `execnb.nbio` together:" |
454 | 531 | ] |
455 | 532 | }, |
456 | 533 | { |
457 | 534 | "cell_type": "code", |
458 | 535 | "execution_count": null, |
459 | 536 | "metadata": {}, |
460 | | - "outputs": [], |
461 | | - "source": [ |
462 | | - "#|export\n", |
463 | | - "def new_nb():\n", |
464 | | - " \"Returns an empty new notebook\"\n", |
465 | | - " return dict2nb({'cells':[],'metadata':{},'nbformat':4,'nbformat_minor':5})" |
466 | | - ] |
467 | | - }, |
468 | | - { |
469 | | - "cell_type": "markdown", |
470 | | - "metadata": {}, |
| 537 | + "outputs": [ |
| 538 | + { |
| 539 | + "name": "stdout", |
| 540 | + "output_type": "stream", |
| 541 | + "text": [ |
| 542 | + "[{'cell_type': 'code', 'metadata': {}, 'source': 'print(1)', 'idx_': 0}]\n" |
| 543 | + ] |
| 544 | + } |
| 545 | + ], |
471 | 546 | "source": [ |
472 | | - "Use this function when creating a new notebook. Useful for when you don't want to create a notebook on disk first and then read it." |
| 547 | + "nb = new_nb([mk_cell('print(1)')])\n", |
| 548 | + "path = Path('test.ipynb')\n", |
| 549 | + "write_nb(nb, path)\n", |
| 550 | + "nb2 = read_nb(path)\n", |
| 551 | + "print(nb2.cells)\n", |
| 552 | + "path.unlink()" |
473 | 553 | ] |
474 | 554 | }, |
475 | 555 | { |
|
486 | 566 | "outputs": [], |
487 | 567 | "source": [ |
488 | 568 | "#|hide\n", |
489 | | - "#|eval: false\n", |
490 | | - "from nbdev.doclinks import nbdev_export\n", |
| 569 | + "from nbdev import nbdev_export\n", |
491 | 570 | "nbdev_export()" |
492 | 571 | ] |
493 | 572 | }, |
|
0 commit comments