Python support for Blocknote.js
Project description
blocknote-py
A Python library for working with Blocknote.js blocks. Convert between dictionaries, markdown, and Blocknote block objects with full type safety and validation.
Features
- Type-safe Blocknote blocks: Pydantic models with full validation
- Bidirectional conversion: Convert between dictionaries, markdown, and Blocknote blocks
- Dictionary conversion: Convert dictionaries to validated Block objects
- Markdown parsing: Parse markdown into Blocknote blocks
- Markdown generation: Convert Blocknote blocks back to markdown
- Text styling support: Bold and italic text formatting
- Lists: Both ordered and unordered lists
- Extensible architecture: Easy to add new block types and converters
- Comprehensive testing: Full test coverage with pytest
Installation
uv add blocknote-py
Or install from source:
git clone https://github.com/yourusername/blocknote-py.git
cd blocknote-py
uv sync --dev
Quick Start
Basic Usage
from blocknote.converter import dict_to_blocks, markdown_to_blocks, blocks_to_markdown, blocks_to_dict
from blocknote.schema import Block, InlineContent
# Convert dictionaries to blocks
data = [
{
"id": "1",
"type": "paragraph",
"content": [
{"type": "text", "text": "Hello, "},
{"type": "text", "text": "world!", "styles": {"bold": True}},
],
}
]
blocks = dict_to_blocks(data)
print(blocks[0].content[0].text) # "Hello, "
# Parse markdown with basic formatting
markdown = """# Hello World
This is **bold** and *italic* text.
- Bullet item 1
- Bullet item 2
1. Numbered item 1
2. Numbered item 2
"""
blocks = markdown_to_blocks(markdown)
print(f"Parsed {len(blocks)} blocks")
# Convert blocks back to markdown
back_to_md = blocks_to_markdown(blocks)
print("Round-trip conversion successful!")
Working with Block Objects
from blocknote.schema import Block, InlineContent
# Create blocks programmatically
block = Block(
id="unique-id",
type="paragraph",
content=[
InlineContent(type="text", text="Hello ", styles={}),
InlineContent(type="text", text="world", styles={"bold": True})
],
props={},
children=[]
)
print(block.type) # "paragraph"
print(len(block.content)) # 2
API Reference
Core Classes
Block
Represents a Blocknote block with content and children.
id: str- Unique identifiertype: BlockType- Block type (paragraph, heading, bulletListItem, orderedListItem)props: Dict[str, Any]- Block-specific propertiescontent: Union[str, List[InlineContent]]- Block contentchildren: List[Block]- Child blocks
InlineContent
Represents inline content within a block.
type: InlineContentType- Content type (currently only "text")text: str- The text contentstyles: Dict[str, Any]- Text styling (bold, italic, etc.)
Converter Functions
dict_to_blocks(data: List[Dict[str, Any]]) -> List[Block]
Converts a list of dictionaries to validated Block objects.
from blocknote.converter import dict_to_blocks
data = [{"id": "1", "type": "paragraph", "content": "Hello"}]
blocks = dict_to_blocks(data)
blocks_to_dict(blocks: List[Block]) -> List[Dict[str, Any]]
Converts a list of Block objects to dictionaries.
from blocknote.converter import blocks_to_dict
dict_data = blocks_to_dict(blocks)
markdown_to_blocks(markdown: str) -> List[Block]
Converts a markdown string to a list of Block objects.
from blocknote.converter import markdown_to_blocks
blocks = markdown_to_blocks("# Hello\n\nWorld")
blocks_to_markdown(blocks: List[Block]) -> str
Converts a list of Block objects to a markdown string.
from blocknote.converter import blocks_to_markdown
markdown = blocks_to_markdown(blocks)
Supported Block Types
- Paragraph: Basic text paragraphs with styling support
- Heading: Headings with levels (H1, H2, H3)
- Bullet Lists: Unordered lists with list items
- Numbered Lists: Ordered lists with list items
- Check Lists: Checklist items with checked/unchecked state
- Toggle Lists: Collapsible list items
- Quotes: Blockquote text blocks
- Tables: Table structures (basic support)
Supported Text Formatting
- Bold text:
**bold**or__bold__ - Italic text:
*italic*or_italic_ - Plain text: Regular text content
Development
Setup
# Clone the repository
git clone https://github.com/yourusername/blocknote-py.git
cd blocknote-py
# Install dependencies (including development tools)
uv sync --dev
# Setup pre-commit hooks (optional)
uv run pre-commit install
Code Quality
This project uses several tools to maintain code quality:
Available Commands
# Run tests
make test
# Check code quality (lint + format check + type check)
make check-all
# Auto-fix formatting and import issues
make fix-all
# Run individual tools
make lint # flake8
make format # black + isort
make type-check # mypy
# Development setup
make setup-dev # Install everything + setup pre-commit
Or using hatch environments:
# Check all
hatch run check-all
# Fix all
hatch run fix-all
# Individual tools
hatch run lint
hatch run format
hatch run type-check
Project Structure
src/
├── blocknote/
│ ├── __init__.py # Main package exports
│ ├── schema/
│ │ ├── __init__.py # Schema exports
│ │ └── types.py # Pydantic models
│ └── converter/
│ ├── __init__.py # Converter exports
│ ├── dict_to_blocknote.py # Dict conversion
│ ├── md_to_blocknote.py # Markdown conversion
│ └── __tests__/ # Tests
│ ├── test_dict_to_blocknote.py
│ └── test_md_to_blocknote.py
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Related Projects
- Blocknote.js - The original JavaScript library
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 blocknote_py-0.1.0.tar.gz.
File metadata
- Download URL: blocknote_py-0.1.0.tar.gz
- Upload date:
- Size: 100.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f8a883946a220ab52a4adc35e7a2db9ed6fcc770ec69b26a3413c8a3b249b766
|
|
| MD5 |
081aeed72992a4dc2ca4b63c8b5490ae
|
|
| BLAKE2b-256 |
1a2e28f66c9e52ba3b15505f49ebefc61609f0d4fd0a5771868bf9993dbfa644
|
File details
Details for the file blocknote_py-0.1.0-py3-none-any.whl.
File metadata
- Download URL: blocknote_py-0.1.0-py3-none-any.whl
- Upload date:
- Size: 28.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5537f09e3c5d57d7399822edfd6b44942dbd272fb639aebe08a9b230bba8d327
|
|
| MD5 |
539171b91fa8760ebd31f53fd538b946
|
|
| BLAKE2b-256 |
eb0d5121eb43695f7572609c74d77f43d5db52e55e27aa4815a88b26509923a6
|