Files
flask_blog/edit_script/edit_entry.ipynb
2026-03-13 19:31:46 +01:00

5.3 KiB

In [2]:
import datetime

def get_entry(file_path, id):

    # 1. Import the current list to get the latest ID

    # We use a namespace to avoid path issues

    namespace = {}

    with open(file_path, 'r') as f:

        exec(f.read(), namespace)

        posts = namespace.get('BLOG_POSTS', [])

    for items in posts:

        if items.get("id") == id:

            content = items.get('content')

            print(content)

            return items 



def update_entry(file_path, post_id, new_content):
    # 1. Load the existing data into memory
    namespace = {}
    with open(file_path, 'r') as f:
        exec(f.read(), namespace)
    
    posts = namespace.get('BLOG_POSTS', [])
    
    # 2. Find and update the specific post
    found = False
    for post in posts:
        if post.get("id") == post_id:
            post['content'] = new_content
            # Optional: Update the date or add an "updated" field
            # post['subtitle'] += " (Updated)" 
            found = True
            break
    
    if not found:
        print(f"Error: Post {post_id} not found.")
        return

    # 3. Write the entire list back to the file
    # Using repr() or a loop to format it cleanly
    with open(file_path, 'w') as f:
        f.write("BLOG_POSTS = [\n")
        for p in posts:
                    # Use .get() to provide a default value if the key is missing
                    display_val = p.get('displayall', False) 
                    data_val = p.get('date', "?") 
                    subtitle_val = p.get('subtitle', "") 
                    
                    entry = f"""    {{
                'id': {p['id']},
                'title': "{p['title']}",
                'subtitle': "{subtitle_val}",
                'date': "{data_val}",
                'content': \"\"\"{p['content']}\"\"\",
                "displayall": {display_val}
            }},\n"""
                    f.write(entry)
        f.write("]\n")
    
    print(f"Post {post_id} updated successfully.")

# --- Usage Example ---
if __name__ == "__main__":
    file = '../content/posts.py'
    target_id = 3
    
    # Get the old one first to see it
    old_post = get_entry(file, target_id)
    
    if old_post:
        pass

        new_text="""
          <p>sudo pacman -S archlinux-keyring</p>
          <p>sudo pacman -Sc</p>
          <p>melange:</p>
          <ul>
            <li><a href="/page2/line/lineplot.html">machine learning with failed hyperparams</a></li>
            <li><a href="/page2/dblp/bar/index.html">the dblp dataset in bar plot</a></li>
            <li><a href="/page2/dblp/edge/index.html">the dblp dataset with an example of community</a></li>
            <li><a href="/page2/dblp/full/index.html">the dblp dataset of 5 years as a graph</a></li>
            <li><a href="/page2/dblp/time/index.html">the generated links as a graph</a></li>

          </ul>


        """
        
        
        update_entry(file, target_id, new_text)
            im still struggling:
          </p>
          <p>sudo pacman -S archlinux-keyring</p>
          <p>melange:</p>
          <ul>
            <li><a href="/page2/line/lineplot.html">machine learning with failed hyperparams</a></li>
            <li><a href="/page2/dblp/bar/index.html">the dblp dataset in bar plot</a></li>
            <li><a href="/page2/dblp/edge/index.html">the dblp dataset with an example of community</a></li>
            <li><a href="/page2/dblp/full/index.html">the dblp dataset of 5 years as a graph</a></li>
            <li><a href="/page2/dblp/time/index.html">the generated links as a graph</a></li>

          </ul>

Post 3 updated successfully.