Example #1
0
def send_ajax_request(obj, method, data, callback):
	global user_info
	def oncomplete(req):
		callback(JSON.parse(req.text) if req.status == 200 else {'error':req.text if req.text != '' else '未能从服务器获取信息'})
	if 'user_info' not in data: data['user_info'] = user_info
	post_data = {'object':obj, 'method':method, 'data': JSON.stringify(data)}
	ajax.post('ajax', data=post_data, oncomplete=oncomplete)
Example #2
0
def generate(e):
    url = 'https://comb.karapasa.ru/generate'
    data = {
        'parlist': json.loads(storage['parlist']),
        'exclist': json.loads(storage['exclist'])
    }
    ajax.post(url, data=json.dumps(data), oncomplete=output_data)
Example #3
0
def run_cpython(script_name, next_step):
    """Send an async POST Ajax call to run the CPython script.
    next_step is the function called when the Ajax call is complete."""
    ajax.post('/time_cpython',
        oncomplete=next_step,
        data=script_name,
        timeout=4)
Example #4
0
def test_next():
    global script_num, failed
    script_num += 1
    options = document['files'].options

    if script_num < len(options):

        option = document['files'].options[script_num]
        script_name = option.value

        src = open(option.value).read()
        document['files'].selectedIndex = script_num

        execute(option, src, forward)

    else:
        info = sys.implementation.version
        version = f"{info.major}.{info.minor}.{info.micro}"

        ajax.post("/cgi-bin/store_speed.py",
            data={
                "results": javascript.JSON.stringify(results),
                "version": version,
                "userAgent": window.navigator.userAgent
            })
Example #5
0
    def save_grid(self, ev):
        def on_complete(req):
            if req.status == 200:
                # print(req.text)
                document["update-grid"].className = "ui right primary button"
                document.select("body")[0].toast({
                    "class":
                    "success",
                    "message":
                    "You're using the good framework !"
                })

            else:
                print("error ", req.text)

        ev.target.className = "ui loading button"
        displays = document.select(".displays")
        displays_data = {}
        for display in displays:
            displays_data[display.id] = display.innerHTML
        # print(displays_data)
        ajax.post(
            self.save_gird_url,
            data={
                "displays": JSON.stringify(displays_data),
                "grid_id": self.grid_id,
            },
            oncomplete=on_complete,
            headers={"Content-Type": "application/x-www-form-urlencoded"},
        )
Example #6
0
def run_cpython(script_name, next_step):
    """Send an async POST Ajax call to run the CPython script.
    next_step is the function called when the Ajax call is complete."""
    ajax.post('/time_cpython',
              oncomplete=next_step,
              data=script_name,
              timeout=4)
Example #7
0
 def stop_lpr(ev):
     target_id = ev.target.id
     if ev.target.tagName == "I":
         target_id = ev.target.parent.id
         ev.target.parent.className = "ui loading icon button stop-recorder"
     else:
         ev.target.className = "ui loading icon button stop-recorder"
     camera_id, project_id = target_id.split("/")
     ajax.post(f"/cameras/{camera_id}/stop-recorder",
               data={"project_id": project_id})
Example #8
0
def post_json(url, inp, cb):
    def cb_wrap(rsp):
        if rsp.status in (0, 200):
            cb(rsp.json)
        elif rsp.status == 500:
            alert('API error')

    data = json.dumps(inp, ensure_ascii=False)
    ajax.post('../' + url,
              data=data,
              headers={"Content-Type": "application/json"},
              oncomplete=cb_wrap)
Example #9
0
def login(event):
    username = document["user"].value
    password = document["pass"].value
    if len(username) > 0 and len(password) > 0:
        ajax.post("/login/",
                  headers={"Content-Type": "application/json"},
                  data=json.dumps({
                      "username": username,
                      "password": password
                  }),
                  oncomplete=redirect)
    else:
        document[
            "error"].innerHTML = "You need to enter something into both fields!"
Example #10
0
def test_browser_ajax():
    from browser import document, ajax

    def on_complete(req):
        if req.status == 200 or req.status == 0:
            document["result"].html = req.text
        else:
            document["result"].html = "error " + req.text

    url = ""
    req = ajax.Ajax()
    req.bind('complete', on_complete)
    # send a POST request to the url
    req.open('POST', url, True)
    req.set_header('content-type', 'application/x-www-form-urlencoded')
    # send data as a dictionary
    req.send({'x': 0, 'y': 1})

    from browser import document, ajax

    def on_complete(req):
        if req.status == 200:
            document["result"].html = req.text
        else:
            document["result"].html = "error " + req.text

    ajax.post(url, data={'x': 0, 'y': 1}, oncomplete=on_complete)

    from browser import ajax

    def read(f):
        data = f.read()
        assert isinstance(data, bytes)

    req = ajax.get("tests.zip", mode="binary", oncomplete=read)

    from browser import ajax, bind, document

    def upload_ok(req):
        print("all right")

    @bind("#upload", "click")
    def uploadfiles(event):
        for f in document["choosefiles"].files:
            ajax.file_upload("/cgi-bin/savefile.py", f, oncomplete=upload_ok)
        assert True

    uploadfiles(evt)  ## added
Example #11
0
def ajaxPostJSON(target: str, payload: dict, complete):
    ajax.post(target,
              headers={"Content-Type": "application/json"},
              data=json.dumps(payload),
              oncomplete=complete)
Example #12
0
ajax.get("files/text-latin1.txt",
         mode='binary',
         oncomplete=lambda req: show(req, 0xe9),
         blocking=True)


def read_image(req):
    print('image', len(req.read()))


ajax.get('../brython.png', mode="binary", oncomplete=read_image)
ajax.get('../brython.png', mode="binary", oncomplete=read_image, blocking=True)

# ajax.post
ajax.post("/cgi-bin/post_test.py",
          oncomplete=lambda req: show(req, "bar:38"),
          data={'bar': 38})

ajax.post("/cgi-bin/post_test.py",
          headers={'Content-Type': "application/x-www-form-urlencoded"},
          oncomplete=lambda req: show(req, "bar:38"),
          data={'bar': 38})

# DOM style
req = ajax.Ajax()
req.open("GET", "files/text-latin1.txt")
req.encoding = 'latin-1'
req.bind("complete", lambda req: show(req, "bébé"))
req.send()

req = ajax.Ajax()
Example #13
0
def stop_lpr(ev):
    # print(ev.target.id)
    camera_id, project_id = (ev.target.id).split("/")
    ajax.post(f'/cameras/{camera_id}/stoplpr', data={'project_id': project_id})