Skip to content

Instantly share code, notes, and snippets.

@artificialsoph
Last active September 20, 2018 14:56
Show Gist options
  • Save artificialsoph/dc7f2cf0a7675fa4ac72e095ada0ae1c to your computer and use it in GitHub Desktop.
Save artificialsoph/dc7f2cf0a7675fa4ac72e095ada0ae1c to your computer and use it in GitHub Desktop.
threadwiki: PEP8 python entry to the Shortest Wiki Contest named for the threadsnake
import glob
import os
import time
import markdown2
from flask import Flask, redirect, request
app = Flask(__name__)
@app.route("/<page>")
def show(page):
path = f"pages/{page}"
if not os.path.exists(path):
return redirect(f"/{page}/edit")
return f"""<h1><a href='/{page}/back'>{page}</a></h1>
{markdown2.markdown_path(path)}<a href='/{page}/edit'>edit</a>
last edit was {time.ctime(os.path.getmtime(path))}"""
@app.route("/<page>/edit", methods=['POST', 'GET'])
def edit(page):
path = f"pages/{page}"
if request.method == 'POST':
open(path, 'w').write(request.form['new'])
return redirect(f"/{page}")
return f"""<h1>Edit {page}</h1><form method=post><textarea name=new>{open(
path, 'r').read() if os.path.exists(path) else ""}</textarea>
<input type=submit></form>"""
@app.route("/<page>/back")
def back(page):
backs = [f"<li><a href='/{file[6:]}'>{file[6:]}</a></li>" for file in
glob.glob("pages/*") if f"/{page}" in open(file, "r").read()]
return f"<h1>Links to {page}</h1><ul>{''.join(backs)}</ul>"
if __name__ == '__main__':
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment