|
1 | 1 | import cmath |
2 | 2 | import math |
3 | 3 | import os |
4 | | -import random |
5 | 4 | import re |
6 | 5 | import tempfile |
7 | | -import time |
8 | 6 | from copy import copy |
9 | 7 | from itertools import islice |
10 | 8 | from types import GeneratorType |
|
35 | 33 | from sage.misc.functional import round |
36 | 34 | from sage.structure.element import Element |
37 | 35 |
|
38 | | -from lmfdb.app import app, is_beta, is_debug_mode, _url_source |
| 36 | +from lmfdb.app import is_beta, is_debug_mode, _url_source |
39 | 37 |
|
40 | 38 |
|
41 | 39 | def integer_divisors(n): |
@@ -802,124 +800,6 @@ def flash_success(msg, *args): |
802 | 800 | flash(Markup(msg % tuple("<span style='color:black'>%s</span>" % escape(x) for x in args)), "success") |
803 | 801 |
|
804 | 802 |
|
805 | | -################################################################################ |
806 | | -# Ajax utilities |
807 | | -################################################################################ |
808 | | - |
809 | | -# LinkedList is used in Ajax below |
810 | | -class LinkedList(): |
811 | | - __slots__ = ('value', 'next', 'timestamp') |
812 | | - |
813 | | - def __init__(self, value, nxt): |
814 | | - self.value = value |
815 | | - self.next = nxt |
816 | | - self.timestamp = time.time() |
817 | | - |
818 | | - def append(self, value): |
819 | | - self.next = LinkedList(value, self) |
820 | | - return self.next |
821 | | - |
822 | | - |
823 | | -class AjaxPool(): |
824 | | - def __init__(self, size=1e4, expiration=3600): |
825 | | - self._size = size |
826 | | - self._key_list = self._head = LinkedList(None, None) |
827 | | - self._expiration = expiration |
828 | | - self._all = {} |
829 | | - |
830 | | - def get(self, key, value=None): |
831 | | - return self._all.get(key, value) |
832 | | - |
833 | | - def __contains__(self, key): |
834 | | - return key in self._all |
835 | | - |
836 | | - def __setitem__(self, key, value): |
837 | | - self._key_list = self._key_list.append(key) |
838 | | - self._all[key] = value |
839 | | - |
840 | | - def __getitem__(self, key): |
841 | | - res = self._all[key] |
842 | | - self.purge() |
843 | | - return res |
844 | | - |
845 | | - def __delitem__(self, key): |
846 | | - del self._all[key] |
847 | | - |
848 | | - def pop_key(self): |
849 | | - head = self._head |
850 | | - if head.next is None: |
851 | | - return None |
852 | | - else: |
853 | | - key = head.value |
854 | | - self._head = head.next |
855 | | - return key |
856 | | - |
857 | | - def purge(self): |
858 | | - if self._size: |
859 | | - while len(self._all) > self._size: |
860 | | - key = self.pop_key() |
861 | | - if key in self._all: |
862 | | - del self._all[key] |
863 | | - if self._expiration: |
864 | | - oldest = time.time() - self._expiration |
865 | | - while self._head.timestamp < oldest: |
866 | | - key = self.pop_key() |
867 | | - if key in self._all: |
868 | | - del self._all[key] |
869 | | - |
870 | | - |
871 | | -pending = AjaxPool() |
872 | | -def ajax_url(callback, *args, **kwds): |
873 | | - if '_ajax_sticky' in kwds: |
874 | | - _ajax_sticky = kwds.pop('_ajax_sticky') |
875 | | - else: |
876 | | - _ajax_sticky = False |
877 | | - if not isinstance(args, tuple): |
878 | | - args = args, |
879 | | - nonce = hex(random.randint(0, 1 << 128)) |
880 | | - pending[nonce] = callback, args, kwds, _ajax_sticky |
881 | | - return url_for('ajax_result', id=nonce) |
882 | | - |
883 | | - |
884 | | -@app.route('/callback_ajax/<id>') |
885 | | -def ajax_result(id): |
886 | | - if id in pending: |
887 | | - f, args, kwds, _ajax_sticky = pending[id] |
888 | | - if not _ajax_sticky: |
889 | | - del pending[id] |
890 | | - return f(*args, **kwds) |
891 | | - else: |
892 | | - return "<expired>" |
893 | | - |
894 | | - |
895 | | -def ajax_more(callback, *arg_list, **kwds): |
896 | | - from .web_display import web_latex |
897 | | - inline = kwds.get('inline', True) |
898 | | - text = kwds.get('text', 'more') |
899 | | - nonce = hex(random.randint(0, 1 << 128)) |
900 | | - if inline: |
901 | | - args = arg_list[0] |
902 | | - arg_list = arg_list[1:] |
903 | | - if isinstance(args, tuple): |
904 | | - res = callback(*arg_list) |
905 | | - elif isinstance(args, dict): |
906 | | - res = callback(**args) |
907 | | - else: |
908 | | - res = callback(args) |
909 | | - res = web_latex(res) |
910 | | - else: |
911 | | - res = '' |
912 | | - if arg_list: |
913 | | - url = ajax_url(ajax_more, callback, *arg_list, inline=True, text=text) |
914 | | - return """<span id='%(nonce)s'>%(res)s <a onclick="$('#%(nonce)s').load('%(url)s', function() { renderMathInElement($('#%(nonce)s').get(0),katexOpts);}); return false;" href="#">%(text)s</a></span>""" % locals() |
915 | | - else: |
916 | | - return res |
917 | | - |
918 | | - |
919 | | -def image_src(G): |
920 | | - return ajax_url(image_callback, G, _ajax_sticky=True) |
921 | | - |
922 | | - |
923 | 803 | def image_callback(G): |
924 | 804 | P = G.plot() |
925 | 805 | _, filename = tempfile.mkstemp('.png') |
|
0 commit comments