Giter Site home page Giter Site logo

trilium-py's Introduction

๐Ÿ trilium-py

Python client for ETAPI of Trilium Note.

Downloads Supported Versions Supported Versions PyPI license Maintenance


๐Ÿฆฎ Table of Contents

๐Ÿ”ง Installation

python3 -m pip install trilium-py --user

๐Ÿ“– (Basic) Usage

These are basic function that Trilium's ETAPI provides. Down below are some simple example code to use this package.

๐Ÿš€ Initialization

If you have a ETAPI token, change the server_url and token to yours.

from trilium_py.client import ETAPI

server_url = 'http://localhost:8080'
token = 'YOUR_TOKEN'
ea = ETAPI(server_url, token)

If you haven't created ETAPI token, you can create one with your password. Please note, you can only see this token once, please save it if you want to reuse the token.

from trilium_py.client import ETAPI

server_url = 'http://localhost:8080'
password = '1234'
ea = ETAPI(server_url)
token = ea.login(password)
print(token)

After initialization, you can use Trilium ETAPI with python now. The following are some examples.

๐Ÿ“Š Application Information

To start with, you can get the application information like this.

print(ea.app_info())

It should give you the version of your server application and some extra information.

๐Ÿ” Search note

Search note with keyword.

res = ea.search_note(
    search="python",
)

for x in res['results']:
    print(x['noteId'], x['title'])

๐Ÿญ Create Note

You can create a simple note like this.

res = ea.create_note(
    parentNoteId="root",
    title="Simple note 1",
    type="text",
    content="Simple note example",
    noteId="note1"
)

The noteId is not mandatory, if not provided, Trilium will generate a random one. You can retrieve it in the return.

noteId = res['note']['noteId']

๐Ÿ–ผ๏ธ Create Image note

Image note is a special kind of note. You can create an image note with minimal information like this. The image_file refers to the path of image.

res = ea.create_image_note(
    parentNoteId="root",
    title="Image note 1",
    image_file="shield.png",
)

๐Ÿ‘€ Get note

To retrieve the note's content.

ea.get_note_content("note1")

You can get a note metadata by its id.

ea.get_note(note_id)

๐Ÿ”„ Update note

Update note content

ea.update_note_content("note1", "updated by python")

Modify note title

ea.patch_note(
    noteId="note1",
    title="Python client moded",
)

๐Ÿ—‘๏ธ Delete note

Simply delete a note by id.

ea.delete_note("note1")

๐Ÿ“… Day note

You can get the content of a certain date with get_day_note. The date string should be in format of "%Y-%m-%d", e.g. " 2022-02-25".

es.get_day_note("2022-02-25")

Then set/update a day note with set_day_note. The content should be a (html) string.

self.set_day_note(date, new_content)

๐Ÿ“ค Export note

Export note comes in two formats html or markdown/md.

res = ea.export_note(
    noteId='sK5fn4T6yZRI',
    format='md',
    savePath='/home/nate/data/1/test.zip',
)

๐Ÿ’พ Create data backup

This example will create a database backup file like this trilium-data/backup/backup-test.db.

res = ea.backup("test")

(Advanced Usage) โœ… TODO List

With the power of Python, I have expanded the basic usage of ETAPI. You can do something with todo list now.

Add TODO item

You can use add_todo to add a TODO item, param is the TODO description

ea.add_todo("ไนฐๆš–ๅฎๅฎ")

Check/Uncheck a TODO item

param is the index of the TODO item

ea.todo_check(0)
ea.todo_uncheck(1)

Update a TODO item

Use update_todo to update a TODO item description at certain index.

ea.update_todo(0, "ๅŽป็ ๅคดๆ•ด็‚น่–ฏๆก")

Delete a TODO item

Remove a TODO item by its index.

ea.delete_todo(1)

Move yesterday's unfinished todo to today

As the title suggests, you can move yesterday's unfinished things to today. Unfinished todo's will be deleted from yesterday's note.

ea.move_yesterday_unfinished_todo_to_today()

(Advanced Usage) ๐Ÿšš Upload Markdown files

Upload single Markdown file with images

You can import Markdown file with images into Trilium now! Trilium-py will help you to upload the images and fix the links for you!

res = ea.upload_md_file(
    parentNoteId="root",
    file="./md-demo/manjaro ไฟฎๆ”นcaps lock.md",
)

Bulk upload Markdown files in a folder

You can upload a folder with lots of Markdown files to Trilium and preserve the folder structure!

Import from VNote

Say, upload all the notes from VNote, simply do this:

res = ea.upload_md_folder(
    parentNoteId="root",
    mdFolder="~/data/vnotebook/",
    ignoreFolder=['vx_notebook', 'vx_recycle_bin', 'vx_images', '_v_images'],
)

Import from Joplin

Joplin can be imported effortlessly.

res = ea.upload_md_folder(
    parentNoteId="root",
    mdFolder="/home/nate/data/joplin_data/",
    ignoreFolder=['_resources', ],
)

Import from Logseq

res = ea.upload_md_folder(
    parentNoteId="root",
    mdFolder="/home/nate/data/logseq_data/",
    ignoreFolder=['assets', 'logseq'],
)

Import from Obsidian

Obsidian has a very unique linking system for files. You should use obsidian-export to convert a Obsidian vault to regular Markdown files. Then you should be able to import the note into Trilium with trilium-py.

Convert it first.

obsidian-export /path/to/your/vault /out

Then import just like a normal markdown, trilium-py will handle the images for you.

res = ea.upload_md_folder(
    parentNoteId="root",
    mdFolder="E:/data/out",
)

Import from Youdao Note/ๆœ‰้“ไบ‘็ฌ”่ฎฐ

Youdao does not provide an export feature anymore. Luckily, you can use https://github.com/DeppWang/youdaonote-pull to download your notes and convert them into markdown files. After that, trilium-py should be able to help you import them.

res = ea.upload_md_folder(
    parentNoteId="root",
    mdFolder="/home/nate/gitRepo/youdaonote-pull/out/",
)

Import from Turtl

You need to convert Turtl from json to markdown first. See turtl-to-markdown for details.

Then you can import with trilium-py like this:

res = ea.upload_md_folder(
    parentNoteId="root",
    mdFolder="/home/nate/gitRepo/turtl-to-markdown/out/",
    ignoreFolder=['_resources'],
)

Import from other markdown software

In general, markdown files have variety of standards. You can always try import them with

res = ea.upload_md_folder(
    parentNoteId="root",
    mdFolder="/home/nate/data/your_markdown_files/",
)

If there is any problem, please feel free to create an issue.

(Advanced Usage) ๐ŸŽจ Beautify notes

Because of the constraints imposed by the library utilized by Trilium, imported notes may experience minor formatting problems. These issues include an additional line appearing at the end of code blocks, images becoming integrated with the note content, and the absence of line breaks between headings, resulting in a cramped appearance of the note content.

Here is what you can do to beautify your note.

Beautify a note

Specify a note id to beautify note content.

ea.beautify_note('krm8B9JthNfi')

Beautify a note and its child notes

ea.beautify_sub_notes('tlPuzU2szLJh')

๐Ÿ› ๏ธ Develop

Install with pip egg link to make package change without reinstall.

python -m pip install --user -e .

๐Ÿ”— Original OpenAPI Documentation

The original OpenAPI document is here. You can open it with swagger editor.

trilium-py's People

Contributors

nriver avatar jwhonce avatar unfortunatelyalex avatar asokawu avatar lndldufer avatar rhilip avatar egutierrez-ar avatar perfectra1n avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.