Files
flask_blog/edit_script/edit_entry.ipynb
2026-03-14 11:53:23 +01:00

177 lines
5.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "090c7992",
"metadata": {},
"outputs": [],
"source": [
"# git entry and print\n",
"def get_entry(file_path, id):\n",
" # 1. Import the current list to get the latest ID\n",
" # use a namespace to avoid path issues\n",
" namespace = {}\n",
"\n",
" with open(file_path, \"r\") as f:\n",
" exec(f.read(), namespace)\n",
"\n",
" posts = namespace.get(\"BLOG_POSTS\", [])\n",
"\n",
" for items in posts:\n",
" if items.get(\"id\") == id:\n",
" content = items.get(\"content\")\n",
" print(content)\n",
" return items\n",
"\n",
"\n",
"def update_entry(file_path, post_id, new_content):\n",
" # 1. Load the existing data into memory\n",
" namespace = {}\n",
" with open(file_path, \"r\") as f:\n",
" exec(f.read(), namespace)\n",
"\n",
" posts = namespace.get(\"BLOG_POSTS\", [])\n",
"\n",
" # 2. Find and update the specific post\n",
" found = False\n",
" for post in posts:\n",
" if post.get(\"id\") == post_id:\n",
" post[\"content\"] = new_content\n",
" # Optional: Update the date or add an \"updated\" field\n",
" # post['subtitle'] += \" (Updated)\"\n",
" found = True\n",
" break\n",
"\n",
" if not found:\n",
" print(f\"Error: Post {post_id} not found.\")\n",
" return\n",
"\n",
" # 3. Write the entire list back to the file\n",
" # Using repr() or a loop to format it cleanly\n",
" with open(file_path, \"w\") as f:\n",
" f.write(\"BLOG_POSTS = [\\n\")\n",
" for p in posts:\n",
" # Use .get() to provide a default value if the key is missing\n",
" display_val = p.get(\"displayall\", False)\n",
" data_val = p.get(\"date\", \"?\")\n",
" subtitle_val = p.get(\"subtitle\", \"\")\n",
"\n",
" entry = f\"\"\" {{\n",
" 'id': {p[\"id\"]},\n",
" 'title': \"{p[\"title\"]}\",\n",
" 'subtitle': \"{subtitle_val}\",\n",
" 'date': \"{data_val}\",\n",
" 'content': \\\"\\\"\\\"{p[\"content\"]}\\\"\\\"\\\",\n",
" \"displayall\": {display_val}\n",
" }},\\n\"\"\"\n",
" f.write(entry)\n",
" f.write(\"]\\n\")\n",
"\n",
" print(f\"Post {post_id} updated successfully.\")\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "e94255ff",
"metadata": {},
"outputs": [],
"source": [
"import datetime\n",
"FIELDS = ['title', 'subtitle', 'content', 'displayall']\n",
"# id is incremental\n",
"def add_entry(file_path, title, subtitle, content, display_all=False):\n",
" # 1. Import the current list to get the latest ID\n",
" # We use a namespace to avoid path issues\n",
" namespace = {}\n",
" with open(file_path, 'r') as f:\n",
" exec(f.read(), namespace)\n",
" \n",
" posts = namespace.get('BLOG_POSTS', [])\n",
" new_id = max([p['id'] for p in posts], default=0) + 1\n",
" print(\"new id\", new_id)\n",
" # 2. Format the new entry string\n",
" date_str = datetime.datetime.now().strftime(\"%B %d, %Y\")\n",
" \n",
" new_post_str = f\"\"\"\n",
" {{\n",
" 'id': {new_id},\n",
" 'title': '{title}',\n",
" 'content': \\\"\\\"\\\"\n",
" {content}\n",
"\\\"\\\"\\\",\n",
" 'subtitle': '{subtitle}',\n",
" 'date': '{date_str}',\n",
" \"displayall\": {display_all}\n",
" }},\n",
" \"\"\"\n",
"\n",
" # 3. Append to the file\n",
" # Note: This appends to the END of the file. \n",
" # If your file ends with ']', we need to strip that first.\n",
" with open(file_path, 'r+') as f:\n",
" content_full = f.read().strip()\n",
" # Find the last closing bracket of the list\n",
" if content_full.endswith(']'):\n",
" content_full = content_full.rsplit(']', 1)[0]\n",
" \n",
" f.seek(0)\n",
" f.write(content_full + new_post_str + \"\\n]\")\n",
" f.truncate()\n",
"\n",
" print(f\"Successfully added Post #{new_id}: {title}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "7c80d17a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"new id 10\n",
"Successfully added Post #10: test timeline\n"
]
}
],
"source": [
"FIEL_PATH = \"../content/posts.py\"\n",
"#get_entry(FIEL_PATH, 0)\n",
"# update_entry(FIEL_PATH, target_id, new_text)\n",
"# add_entry(FIEL_PATH, \"title\", \"subtitle\", new_text)\n",
"text=\"\"\"<h1 class=\"text-3xl font-bold mb-8\">Project Timeline</h1>\n",
"\n",
"{% include \"components/timeline.html\" %}\n",
"\"\"\"\n",
"add_entry(FIEL_PATH, \"test timeline\", \"subtitle\", text)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "graphx",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}