0

I am looking to create tool that generates example usage of a requested python module.

Consider the following...

# Code Examples using os module in Python

## Example 1: Get the current working directory using the `os` module

import os

current_working_directory = os.getcwd()
print(f"Current working directory is: {current_working_directory}")

## Example 2: Change the current working directory using the `os` module

import os

path = "/your/desired/directory"
os.chdir(path)

Where # I want this parsed into a section.

Where ## I want this parsed into a markdown cell.

Where ''' I want this parsed into a code cell.

So to clarify, I want to paste a large text blob such as the example above, and the appropriate cells generated with the content expected.

Sorry if this is answered elsewhere, I wasn't sure what to search and tried quite a few before asking.

I asked chatgpt with no luck.
Did some google searches and found something called nbconvert. Doesn't seem to be what I need but could be wrong.

Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
James
  • 35
  • 6

1 Answers1

1

nbdev is set up to do this sort of thing automatically (Quarto might be as well[?]); however, the syntax probably doesn't match what you propose, and you may already have written your input.

If you absolutely maintain your specifications then you can script your own use of this using nbformat in combination with Python to parse your input document and make a notebook to the specifications you describe. nbformat is able to write notebooks and already has the abstractions of cells and cell types baked in and you can easily output new notebooks with contents as you design programmatically. nbformat is an integral part of Jupyter and so if you have Jupyter installed it is already there.

The intro at the top of here will probably help you see how nbformat might be helpful for what you plan. That example is exactly pertinent, but how it makes the product notebook should help you. Additionally, see here and here for more examples with code.

Wayne
  • 6,607
  • 8
  • 36
  • 93