create a command-line blog using Python
Step 1: Set Up the Project
- Create a new directory for your project:
mkdir cli_blog cd cli_blog
- Create a Python file for the blog:
touch blog.py
Step 2: Write the Blog Application
In blog.py
, write the following code:
import os import datetime # File to store blog posts BLOG_FILE = "blog_posts.txt" def clear_screen(): """Clear the terminal screen.""" os.system('cls' if os.name == 'nt' else 'clear') def display_menu(): """Display the main menu.""" print("Welcome to the Command-Line Blog!") print("1. View Blog Posts") print("2. Add a New Post") print("3. Exit") def view_posts(): """Display all blog posts.""" clear_screen() if not os.path.exists(BLOG_FILE): print("No posts found. Add a new post!\n") return with open(BLOG_FILE, "r") as file: posts = file.readlines() if not posts: print("No posts found. Add a new post!\n") else: print("=== Blog Posts ===\n") for i, post in enumerate(posts, 1): print(f"{i}. {post.strip()}") print() def add_post(): """Add a new blog post.""" clear_screen() print("=== Add a New Post ===\n") title = input("Enter the title of your post: ") content = input("Write your post: ") author = input("Enter your name: ") timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") with open(BLOG_FILE, "a") as file: file.write(f"[{timestamp}] {title} by {author}\n") file.write(f"{content}\n\n") print("\nPost added successfully!\n") def main(): """Run the blog application.""" while True: display_menu() choice = input("Choose an option (1-3): ") if choice == "1": view_posts() elif choice == "2": add_post() elif choice == "3": print("Goodbye!") break else: print("Invalid choice. Please try again.\n") if __name__ == "__main__": main()
Step 3: Run the Blog Application
Run the script:
python blog.py
You’ll see a menu with the following options:
- View Blog Posts: Displays all saved blog posts.
- Add a New Post: Allows you to write and save a new blog post.
- Exit: Exits the application.
Step 4: Test the Application
- Choose Option 2 to add a new post. Enter a title, content, and your name.
- Choose Option 1 to view all posts. You’ll see the post you just added.
- Add more posts and view them.
- Choose Option 3 to exit the application.
Step 5: How It Works
- Blog Posts Storage: All posts are stored in a text file (
blog_posts.txt
). Each post includes a timestamp, title, author, and content. - Menu-Driven Interface: The application uses a simple menu to navigate between viewing posts, adding posts, and exiting.
- Clear Screen: The terminal screen is cleared before displaying new content for a better user experience.
Optional Enhancements
- Edit or Delete Posts: Add functionality to edit or delete existing posts.
- Search Posts: Allow users to search for posts by title or author.
- Categories/Tags: Add support for categorizing or tagging posts.
- Export to HTML: Add an option to export all posts to an HTML file for easy sharing.
- Rich Text Formatting: Use a library like
rich
to add colors and formatting to the terminal output.
Example Output
Main Menu:
Welcome to the Command-Line Blog! 1. View Blog Posts 2. Add a New Post 3. Exit Choose an option (1-3):
View Posts:
=== Blog Posts === 1. [2023-10-01 14:30:00] My First Post by John Doe This is my first blog post. I'm excited to start blogging! 2. [2023-10-02 09:15:00] Python Tips by Jane Doe Here are some Python tips for beginners.
Add a New Post:
=== Add a New Post === Enter the title of your post: My Second Post Write your post: This is another blog post about Python. Enter your name: John Doe Post added successfully!