Skip to main content

get_code_from_markdown

Extract code from within triple-quotes (```) in markdown files (code blocks).

Usage

Getting code blocks from markdown files

The language parameter defaults to python. This will find codeblocks that start with ```python

from get_code_from_markdown import *

blocks = get_code_from_markdown(["# get_code_from_markdown", "```python", "print('hello world')", "```"])
# or
blocks = get_code_from_markdown_filename("README.md", language="python")
# or
with open("README.md", "r") as f:
    blocks = get_code_from_markdown_file(f, language="python")

Running code blocks from markdown files

You can use a subprocess or just exec the code to see if it works.

from get_code_from_markdown import run_code_from_markdown_blocks
blocks = ["print('hello world')"]

# run function uses exec() by default
run_code_from_markdown_blocks(blocks)

# you can choose to use subprocess output instead
# this will use python3 by default
run_code_from_markdown_blocks(blocks, method=RunMethods.SUBPROCESS)

# give arguments to specify a different command
run_code_from_markdown_blocks(blocks, method=RunMethods.SUBPROCESS(["python3", "-c"]))

Format of outputted code blocks

The format is a list of \n-delimited strings, each representing a block of code from the parsed markdown file. You can split the code blocks further into individual lines of code with string.split("\n")

Development

  • Use black (code linter)
  • Run pytest (unit tests)
  • Build a package for PyPI: python -m build --sdist --wheel

Legal

This is a derivative work of Codeblocks which provides a simpler version with less dependencies. It is covered by the same Apache license (see LICENSE file in this git repo). Use Codeblocks for its CLI and automatic formatting features.

Metadata

Release files for get-code-from-markdown 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for get-code-from-markdown 1.0.1
File Size Uploaded Provenance
get_code_from_markdown-1.0.1.tar.gz 6.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for get-code-from-markdown 1.0.1
File Interpreter ABI Platform
get_code_from_markdown-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 14.1 kB

Release files / get_code_from_markdown-1.0.1.tar.gz

Download URL get_code_from_markdown-1.0.1.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a25f5f8c2c61928bfdd99624ece963a786c47beb778ccd2741b08b2386fba4e5
BLAKE2b-256 checksum
How to use checksums
5580eb91bb00ab6b90e7c724834a792be6812589f73849dbc8887d230a2ef733
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / get_code_from_markdown-1.0.1-py3-none-any.whl

Download URL get_code_from_markdown-1.0.1-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
323b4567c942670a1bb6c974de115b8910c31553f5e3e8def97c65ddde63aadc
BLAKE2b-256 checksum
How to use checksums
21888309addda38030601eb0b05ef92b16c3fbff022c46b265b2ef8685d7b113
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Security for get-code-from-markdown 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page