Embed python code from script to markdown
Project description
Code Embedder
Seamlessly update code snippets in your README files! 🔄📝🚀
Description • How it works • Setup - Github Action • Setup - Pre-commit Hook • Examples • Contributing • Development
📚 Description
Code Embedder is a GitHub Action and a pre-commit hook that automatically updates code snippets in your markdown (README) files. It finds code blocks in your README that reference specific scripts, then replaces these blocks with the current content of those scripts. This keeps your documentation in sync with your code.
✨ Key features
- 🔄 Automatic synchronization: Keep your
READMEcode examples up-to-date without manual intervention. - 🛠️ Easy setup: Simply add the action to your GitHub workflow / pre-commit hook and format your
READMEcode blocks. - 📝 Section support: Update only specific sections of the script in the
README. This is language agnostic. - 🧩 Object support: Update only specific objects (functions, classes) in the
README. The latest version supports only 🐍 Python objects (other languages to be added soon).
By using Code Embedder, you can focus on writing and updating your actual code 💻, while letting the Code-Embedder take care of keeping your documentation current 📚🔄. This reduces the risk of outdated or incorrect code examples in your project documentation.
🔍 How it works
The Code Embedder scans markdown files for special tags that specify which parts of your scripts to embed. When it finds these tags, it automatically updates the code blocks with the latest content from your source files. When used as a GitHub Action, any changes are automatically committed and pushed to your repository 🚀.
📄 Full script updates
In the README (or other markdown) file, the full script is marked with the following tag:
```language:path/to/script
```
📂 Section updates
In the README (or other markdown) file, the section of the script is marked with the following tag:
```language:path/to/script:s:section_name
```
[!Note] Notice that the
path/to/scriptis followed bys:in the tag to indicate that the sectionsection_nameis being updated.
You must also add the following comment tags in the script file path/to/script, where the section is located:
[Comment sign] code_embedder:section_name start
...
[Comment sign] code_embedder:section_name end
The comment sign is the one that is used in the script file, e.g. # for Python, or // for JavaScript. For example, in python script you add # code_embedder:A start and # code_embedder:A end to new lines to mark the start and end of the section A you want to include in the README.
The section_name must be unique in the file, otherwise the Code-Embedder will use the first section found.
🧩 Object updates
In the README (or other markdown) file, the object of the script is marked with the following tag:
```language:path/to/script:o:object_name
```
[!Note] Notice that the
path/to/scriptis followed byo:in the tag to indicate that the objectobject_nameis being updated.
[!Note] The object name must match exactly the name of the object (function, class) in the script file, including the case (e.g. if class is
Personthen object name must bePerson, notperson). Currently, only 🐍 Python objects are supported.
🔧 Setup - Github Action
Use Code Embedder as a Github Action by adding the following to your .github/workflows/code-embedder.yaml file:
name: Code Embedder
on: pull_request
permissions:
contents: write
jobs:
code_embedder:
name: "Code embedder"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref }}
- name: Run code embedder
uses: kvankova/code-embedder@v1.1.3
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
🔧 Setup - Pre-commit Hook
You can set up Code Embedder as a pre-commit hook using either:
- Installation via PyPI
- Direct repository reference in your
.pre-commit-config.yamlfile
A. Installation via PyPI
Install the package:
pip install code-embedder==v1.1.3
Your .pre-commit-config.yaml file should look like this:
- repo: local
hooks:
- id: code-embedder
entry: code-embedder run
B. Direct repository reference
Alternatively, you can reference the repository directly in your .pre-commit-config.yaml file:
- repo: https://github.com/kvankova/code-embedder
rev: v1.1.3
hooks:
- id: code-embedder
entry: code-embedder run
🔧 Options
Command code-embedder run has the following options:
| Option | Description |
|---|---|
--all-files |
Process all files in the repository. In pre-commit hook, it by default checks only the changed files. |
💡 Examples
📄 Full script update
Let's say you have the following README file:
# README
This is a readme.
```python:main.py
```
The main.py file contains the following code:
print("Embedding successful")
Once code-embedder runs, the code block sections are filled with the content of the script located at main.py and updated in the README file.
# README
This is a readme.
```python:main.py
print("Embedding successful")
```
With any changes to main.py, the code block section is updated in the README file with the next code-embedder run.
📂 Section update
Now we have the following README file:
# README
This is a readme.
```python:main.py:s:A
```
The main.py file contains the following code:
print("Hello, world!")
# code_embedder:A start
print("Embedding successful")
# code_embedder:A end
Once code-embedder runs, the code block section will be updated in the README file with the content of the section A from the script located at main.py (in case of using it as a Github Action, the changes are then pushed to the repository 🚀).
# README
This is a readme.
```python:main.py:s:A
print("Embedding successful")
```
With any changes to the section A in main.py, the code block section is updated in the README file with the next code-embedder run.
🧩 Object update
The tag used for object update follows the same convention as the tag for section update with the following changes:
- use
o:instead ofs: - use
object_name
[!Note] The
object_namemust match exactly the name of the object (function, class) in the script file, including the case. If you define classPersonin the script, you must usePersonas the object name in theREADME, not lowercaseperson.
For example, let's say we have the following README file:
# README
This is a readme.
Function `print_hello` is defined as follows:
```python:main.py:o:print_hello
```
Class `Person` is defined as follows:
```python:main.py:o:Person
```
The main.py file contains the following code:
...
def print_hello():
print("Hello, world!")
...
class Person:
def __init__(self, name):
self.name = name
def say_hello(self):
print(f"Hello, {self.name}!")
...
Once code-embedder runs, the code block section will be updated in the README file with the content of the function print_hello and class Person from the script located at main.py (in case of using it as a Github Action, the changes are then pushed to the repository 🚀).
# README
This is a readme.
Function `print_hello` is defined as follows:
```python:main.py:o:print_hello
def print_hello():
print("Hello, world!")
```
Class `Person` is defined as follows:
```python:main.py:o:Person
class Person:
def __init__(self, name):
self.name = name
def say_hello(self):
print(f"Hello, {self.name}!")
```
With any changes to the function print_hello or class Person in main.py, the code block sections are updated in the README file with the next code-embedder run.
🤝 Contributing
We welcome contributions to improve this package!
- If you have an idea for a new feature ✨, open a new feature request on GitHub.
- If you spot a bug 🐛, open a new issue on GitHub.
- If you want to contribute to the code, please pick an issue that is not assigned to anyone and comment on it, so that we know you are working on it.
🛠️ Development
- Fork this project
- Install poetry
- Install the dependencies by using the following command:
poetry install --with dev
- Make changes to the codebase and run the tests to make sure everything works as expected. ✅
poetry run pytest
- Commit your changes, push them to the repository 🚀, and open a new pull request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file code_embedder-1.1.3.tar.gz.
File metadata
- Download URL: code_embedder-1.1.3.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.11.12 Darwin/23.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
406314cfc9d6454c0ccbe7f21793cb58606857c6a95d5f82d99b74431af34678
|
|
| MD5 |
607eceb5a5d6ef22d1968a2597e4122b
|
|
| BLAKE2b-256 |
abb4330facccc81c72bcab2fae604aec3a2316e2a2b53481e8016d46c51b5a2b
|
File details
Details for the file code_embedder-1.1.3-py3-none-any.whl.
File metadata
- Download URL: code_embedder-1.1.3-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.11.12 Darwin/23.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
976d1bb1e47010e8bca103546ed30a2a91dd5cc1047350abf2276d3f065a0e0b
|
|
| MD5 |
75abc439b7e62196c34c59a76b21a3c6
|
|
| BLAKE2b-256 |
bb25b21ba8f21cf9c8a241339099ecd942257deff74005491fd19dcdcd679bc1
|