The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Create a Python-powered personal wiki

A Python-powered personal wiki can be built using a lightweight web framework like Flask. Below is a simple, functional personal wiki application with features like creating, editing, and viewing pages, with content saved as markdown files.


Python-Powered Personal Wiki (Flask + Markdown)

python
from flask import Flask, render_template_string, request, redirect, url_for, abort import os import markdown app = Flask(__name__) WIKI_DIR = 'wiki_pages' if not os.path.exists(WIKI_DIR): os.makedirs(WIKI_DIR) HTML_TEMPLATE = """ <!DOCTYPE html> <html> <head> <title>{{ title }}</title> <style> body { font-family: Arial, sans-serif; margin: 40px; } a { text-decoration: none; color: #007BFF; } a:hover { text-decoration: underline; } .menu { margin-bottom: 20px; } </style> </head> <body> <div class="menu"> <a href="{{ url_for('index') }}">Home</a> | <a href="{{ url_for('edit_page', page_name=title) }}">Edit</a> | <a href="{{ url_for('create_page') }}">Create New</a> </div> <h1>{{ title }}</h1> <div>{{ content|safe }}</div> </body> </html> """ EDIT_TEMPLATE = """ <!DOCTYPE html> <html> <head> <title>Edit {{ title }}</title> </head> <body> <h1>Edit Page: {{ title }}</h1> <form method="post"> <textarea name="content" rows="20" cols="80">{{ content }}</textarea><br> <input type="submit" value="Save"> </form> <a href="{{ url_for('view_page', page_name=title) }}">Cancel</a> </body> </html> """ CREATE_TEMPLATE = """ <!DOCTYPE html> <html> <head> <title>Create Page</title> </head> <body> <h1>Create New Wiki Page</h1> <form method="post"> <label>Page Name: <input type="text" name="page_name"></label><br><br> <textarea name="content" rows="20" cols="80"></textarea><br> <input type="submit" value="Create"> </form> <a href="{{ url_for('index') }}">Back</a> </body> </html> """ @app.route('/') def index(): pages = [f.replace('.md', '') for f in os.listdir(WIKI_DIR) if f.endswith('.md')] return render_template_string(""" <h1>My Wiki</h1> <ul> {% for page in pages %} <li><a href="{{ url_for('view_page', page_name=page) }}">{{ page }}</a></li> {% endfor %} </ul> <a href="{{ url_for('create_page') }}">Create a New Page</a> """, pages=pages) @app.route('/wiki/<page_name>') def view_page(page_name): filepath = os.path.join(WIKI_DIR, f"{page_name}.md") if not os.path.isfile(filepath): return redirect(url_for('edit_page', page_name=page_name)) with open(filepath, 'r', encoding='utf-8') as file: content_md = file.read() content_html = markdown.markdown(content_md) return render_template_string(HTML_TEMPLATE, title=page_name, content=content_html) @app.route('/edit/<page_name>', methods=['GET', 'POST']) def edit_page(page_name): filepath = os.path.join(WIKI_DIR, f"{page_name}.md") if request.method == 'POST': with open(filepath, 'w', encoding='utf-8') as file: file.write(request.form['content']) return redirect(url_for('view_page', page_name=page_name)) if os.path.isfile(filepath): with open(filepath, 'r', encoding='utf-8') as file: content = file.read() else: content = '' return render_template_string(EDIT_TEMPLATE, title=page_name, content=content) @app.route('/create', methods=['GET', 'POST']) def create_page(): if request.method == 'POST': page_name = request.form['page_name'] if not page_name.isalnum(): return "Invalid page name. Use only letters and numbers.", 400 filepath = os.path.join(WIKI_DIR, f"{page_name}.md") if os.path.exists(filepath): return redirect(url_for('edit_page', page_name=page_name)) with open(filepath, 'w', encoding='utf-8') as file: file.write(request.form['content']) return redirect(url_for('view_page', page_name=page_name)) return render_template_string(CREATE_TEMPLATE) if __name__ == '__main__': app.run(debug=True)

Features

  • Create new pages.

  • Edit existing pages using a Markdown editor.

  • View rendered pages.

  • Markdown-based for portability and ease of formatting.


How to Run

  1. Save the script as wiki.py.

  2. Run the script with python wiki.py.

  3. Visit http://localhost:5000 in your browser.

  4. Your wiki pages will be stored in a wiki_pages folder in .md format.

Let me know if you want to add user authentication, search, or media uploads.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About