from collections import defaultdict, namedtuple, OrderedDict
from contextlib import contextmanager
from enum import Enum
+import http.server
import itertools
import json
from matplotlib import pyplot
+from threading import Thread
import tkinter as tk
from tkinter import ttk
self.view.set_student(name)
-def get_table_bookmarklet(records):
- import html
-
- rows = [
- ['' if v is None else str(v) for v in r] for r in records
- ]
- table_html = (
- '<table style="border:1px solid black;">'
- + '<thead>'
- + ''.join('<th>' + html.escape(h) + '</th>' for h in DataEditModel.Record.DISPLAY_NAMES)
- + '</thead>'
- + '<tbody>'
- + ''.join(
- '<tr>'
- + ''.join('<td>' + v + '</td>' for v in row)
- + '</tr>'
- for row in rows
- )
- + '</tbody>'
- + '</table>'
- )
- assert "'" not in table_html
- js_table_literal = "'{}'".format(table_html)
- return (
- 'javascript:(function(){document.querySelector(\'[contenteditable=\\\'true\\\']\')'
- + '.innerHTML+=\'<br/>\'+' + js_table_literal + '+\'<br/>\';})()'
- )
+class AsyncHttpServer:
+ def __init__(self, port, data):
+ class Handler(http.server.BaseHTTPRequestHandler):
+ def __init__(self): pass
+ def __call__(self, *args): super().__init__(*args)
+
+ def do_GET(self):
+ self.send_response(200)
+ self.send_header('Access-Control-Allow-Origin', '*')
+ self.end_headers()
+ self.wfile.write(data)
+
+ self.server = http.server.HTTPServer(('localhost', port), Handler())
+
+ def start(self):
+ Thread(target = lambda: self.server.serve_forever()).start()
+ def stop(self):
+ self.server.shutdown()
class MainView(View):
@contextmanager
def on_gen_table():
name = self.controller.state['student']
- command = get_table_bookmarklet(self.controller.data[name])
- self.root.clipboard_clear()
- self.root.clipboard_append(command)
+ rows = DataEditModel.Record.DISPLAY_NAMES + self.controller.data[name]
+ data = json.dumps(rows).encode('utf-8')
+ server = AsyncHttpServer(57853, data)
+ server.start()
messagebox.showinfo(
- "Table Command Copied",
- "In Gmail, make sure you have a new message box open,"
- + " then paste the command into the address bar and press Enter."
- + "\nMake sure \"javascript:\" shows up at the beginning of the address bar."
- ,
+ "Serving Table Data",
+ "Table data is ready to be transferred to your browser. You may close this dialog when the transfer is complete.",
)
- self.gen_table_btn = ttk.Button(actions_frame, text = "Generate Gmail table", command = on_gen_table)
+ server.stop()
+ self.gen_table_btn = ttk.Button(actions_frame, text = "Export table", command = on_gen_table)
self.student_sel.pack(**TK_PAD, side = tk.LEFT)
add_student_btn.pack(**TK_PAD, side = tk.LEFT)