from router import route from utils import render_template, parse_query from database import db from config import SERVER_HOST, SERVER_PORT @route('/update', ['POST']) def update_post(query, *args): expressions, conditions = parse_query(query) cursor = db.cursor() cursor.execute('UPDATE `table_task1` SET {} WHERE {}'.format( expressions, conditions )) result = cursor.fetchall() cursor.close() return f'

UPDATE: {result}

' @route('/delete', ['POST']) def delete_post(query, *args): return f'

DELETE: {query}

' @route('/add', ['POST']) def add_post(query, *args): return f'

ADD: {query}

' @route('/') def index_get(query, *args): cursor = db.cursor() cursor.execute('DESCRIBE table_task1;') table_structure = cursor.fetchall() cursor.execute('SELECT * FROM table_task1;') content = cursor.fetchall() cursor.close() heading = [] for column in [field[0] for field in table_structure]: heading.append(f'{column}') heading = '\n%s\n' % '\n'.join(heading) rows = [] for row_index, row in enumerate(content): formatted = [] for field_index, field in enumerate(row): color = 'odd' if (field_index % 2 + row_index % 2) % 2 == 0 else 'even' formatted.append(f'{field}') rows.append('{}'.format(''.join(formatted))) body = '{}'.format("\n".join(rows)) data = render_template('index.html', heading=heading, body=body) return data if __name__ == '__main__': from server import start_server start_server(SERVER_HOST, SERVER_PORT)