Skip to content

Instantly share code, notes, and snippets.

@elmimmo
Last active February 14, 2025 11:22
Show Gist options
  • Save elmimmo/88bf5a2c51c1f7c1f1d4c52a5983f53e to your computer and use it in GitHub Desktop.
Save elmimmo/88bf5a2c51c1f7c1f1d4c52a5983f53e to your computer and use it in GitHub Desktop.
Export snippets stored in Dash by kapeli.com to text files
import sqlite3
import os
# Connect to the SQLite database
db_path = "dash snippets.dash" # Path to the SQLite database file
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Query to retrieve records from the "snippets" table
cursor.execute("SELECT sid, title, body, syntax FROM snippets")
snippets = cursor.fetchall()
# Loop through the records and export to text documents
for snippet in snippets:
sid, title, body, syntax = snippet
# Clean the folder name to avoid special characters
syntax_folder = syntax.replace("/", "_")
output_folder = os.path.join("exported_snippets", syntax_folder)
os.makedirs(output_folder, exist_ok=True)
# Clean the file name to avoid special characters
file_name = f"{title}.md"
file_name = "".join(x for x in file_name if x.isalnum() or x.isspace() or x in ['.', '-'])
file_name = os.path.join(output_folder, file_name)
with open(file_name, "w") as file:
file.write("---\ntags:\n")
# Query to retrieve tags related to the snippet
cursor.execute("SELECT tag FROM tags WHERE tid IN (SELECT tid FROM tagsIndex WHERE sid = ?)", (sid,))
tags = cursor.fetchall()
for tag in tags:
file.write(f" - {tag[0]}\n")
file.write("---\n")
# Start the second block with appropriate syntax
file.write("```")
if syntax != "None":
file.write(syntax.lower())
file.write("\n")
file.write(body)
file.write("\n```")
# Close the database connection
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment