from flask import Flask, request, redirect app = Flask(__name__) nextId = 4 topics = [ {'id': 1, 'title': 'html', 'body': 'html is ...'}, {'id': 2, 'title': 'css', 'body': 'css is ...'}, {'id': 3, 'title': 'javascript', 'body': 'javascript is ...'} ] def template(contents, content, id=None): contextUI = '' if id != None: contextUI = f'''
  • update
  • ''' return f'''

    WEB

      {contents}
    {content} ''' def getContents(): liTags = '' for topic in topics: liTags = liTags + f'
  • {topic["title"]}
  • ' return liTags @app.route('/') def index(): return template(getContents(), '

    Welcome

    Hello, WEB') @app.route('/read//') def read(id): title = '' body = '' for topic in topics: if id == topic['id']: title = topic['title'] body = topic['body'] break return template(getContents(), f'

    {title}

    {body}', id) @app.route('/create/', methods=['GET', 'POST']) def create(): if request.method == 'GET': content = '''

    ''' return template(getContents(), content) elif request.method == 'POST': global nextId title = request.form['title'] body = request.form['body'] newTopic = {'id': nextId, 'title': title, 'body': body} topics.append(newTopic) url = '/read/'+str(nextId)+'/' nextId = nextId + 1 return redirect(url) @app.route('/update//', methods=['GET', 'POST']) def update(id): if request.method == 'GET': title = '' body = '' for topic in topics: if id == topic['id']: title = topic['title'] body = topic['body'] break content = f'''

    ''' return template(getContents(), content) elif request.method == 'POST': global nextId title = request.form['title'] body = request.form['body'] for topic in topics: if id == topic['id']: topic['title'] = title topic['body'] = body break url = '/read/'+str(id)+'/' return redirect(url) @app.route('/delete//', methods=['POST']) def delete(id): for topic in topics: if id == topic['id']: topics.remove(topic) break return redirect('/') app.run(debug=True)