You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Auto-saves all changes when context exitswithLogseqClient('/path/to/graph') asclient:
client.add_journal_entry('This will be auto-saved!')
client.create_page('New Page', 'Content here #demo')
# No need to call save - happens automatically
With Backup Protection
# Creates backup before making changeswithLogseqClient('/path/to/graph', backup_on_enter=True) asclient:
client.add_journal_entry('Changes are protected by backup')
# If exception occurs, can rollback to backup
Manual Save Control
# Disable auto-save for manual controlwithLogseqClient('/path/to/graph', auto_save=False) asclient:
client.add_journal_entry('Changes tracked but not auto-saved')
# Check what's been modifiedsession_info=client.get_session_info()
print(f"Modified {session_info['modified_pages']} pages")
# Save manually when readysaved_count=client.save_all()
try:
withLogseqClient('/path', backup_on_enter=True) asclient:
client.create_page('Test', 'Content')
raiseValueError("Something went wrong!") # Simulated errorexceptValueError:
# Context manager will offer rollback option if backup was createdpass
Basic Usage
Connect to a Logseq Graph
# Connect to your Logseq graphclient=LogseqClient('/path/to/your/logseq/graph')
graph=client.load_graph()
# Get basic statisticsstats=graph.get_statistics()
print(stats)
Working with Pages
# Access pages dictionarypages=graph.pages# Get a specific pagepage=pages['page_name']
# Create a new pagenew_page=client.create_page('New Page', 'Initial content here')
# Add journal entryjournal=client.add_journal_entry('Today I learned about Logseq Python! #learning')
Working with Blocks
# Create a new blockblock=Block(content='This is a new block with #tags')
# Check what was extractedprint(f"Tags: {block.tags}")
print(f"Task state: {block.task_state}")
# Create task blockstask_block=Block(content='TODO Finish the project [#A]')
print(f"Task: {task_block.task_state}")
print(f"Priority: {task_block.priority}")
# Work with sample dataforblockinsample_blocks:
print(f"Content: {block.content}")
ifblock.is_task():
print(f" Task State: {block.task_state}")
ifblock.tags:
print(f" Tags: {', '.join(block.tags)}")
# Show welcome screen againshow_welcome()
# Show examples again show_examples()
# Get help on any componenthelp(LogseqClient)
help(Block)
help(Page)
# Explore sample datasample_blocks[0].contentsample_pages[0].name# Check what methods are availabledir(graph)
dir(client)
Graph Analysis
# Get statisticsstats=graph.get_statistics()
# Find all pages with tagstagged_pages= [pforpingraph.pages.values() ifp.tags]
# Find all task blockstask_blocks= [bforbingraph.blocks.values() ifb.is_task()]
# Find completed taskscompleted= [bforbingraph.blocks.values() ifb.is_completed_task()]
# Add custom properties to blocksblock=Block(content='My block')
block.properties['priority'] =Priority.Ablock.properties['custom_field'] ='custom_value'
Saving Changes to Filesystem
Quick Save Methods
# Save helper functions (from startup script)show_save_methods() # Show all save options# Save all pages in the graphsave_graph(client)
# Quick add task to today's journaltask=quick_add_task(client, 'TODO Review the new features #work')
# Preview markdown before savingpreview_markdown(page)
preview_markdown(block)
Direct Client Methods
# Update existing block content (auto-saves)updated_block=client.update_block(block_id, 'Updated content #new-tag')
# Add new blocks to existing pages (auto-saves)new_block=client.add_block_to_page('Project Planning', 'New milestone achieved!')
# Create new pages (auto-saves)new_page=client.create_page('Meeting Notes', 'Initial content here #meeting')
# Add journal entries (auto-saves)journal=client.add_journal_entry('Today I made great progress! #success')
# Delete blocks (auto-saves)success=client.delete_block(block_id)
Manual Save Operations
# Save individual pagesclient._save_page(page)
# Save after modifying blocks manuallyblock.content='Modified content'block.task_state=TaskState.DONEpage=client.get_page(block.page_name)
client._save_page(page) # Save the page containing the block
Working with File Paths
# Check where pages will be savedforpage_name, pageingraph.pages.items():
ifpage.file_path:
print(f'{page_name}: {page.file_path}')
# Create page with specific pathfrompathlibimportPathfile_path=client.graph_path/'pages'/'custom-page.md'page=Page(name='Custom Page', file_path=file_path)
client._save_page(page)
Batch Operations
# Modify multiple blocks and save all affected pagesmodified_pages=set()
forblockingraph.blocks.values():
if'old-tag'inblock.tags:
block.content=block.content.replace('#old-tag', '#new-tag')
ifblock.page_name:
modified_pages.add(block.page_name)
# Save all modified pagesforpage_nameinmodified_pages:
page=graph.get_page(page_name)
ifpage:
client._save_page(page)
Tips
Use tab completion to explore available methods
Check the sample_blocks and sample_pages variables for example data
The library automatically parses tags, task states, priorities, and dates from content
Most client methods (add_journal_entry, create_page, update_block) auto-save
Use preview_markdown() to see how content will look before saving
Changes to objects in memory need explicit saving with client._save_page()
Use save_graph(client) to save all pages at once
Use graph.get_statistics() to understand your graph structure