# This is slighly modified from the console.py used in Brython's console # The file should be called console.py, but Neocities currently does not allow uploading *.py import sys import time import random import dis import traceback #doctype html 5 causes issues with setting height of the container #so python to the rescue! :) import pydom _height=doc.documentElement.clientHeight _s=pydom.Selector('#container') #set height of container to 66% of screen _s.get().height('%spx' % int(_height*0.66)) _rand=random.random() has_ace = True try: editor=JSObject(ace).edit("editor") editor.getSession().setMode("ace/mode/python") except: import html editor = html.TEXTAREA(rows=20,cols=70) doc["editor"] <= editor def get_value(): return editor.value def set_value(x):editor.value=x editor.getValue = get_value editor.setValue = set_value has_ace = False if sys.has_local_storage: from local_storage import storage else: storage = False def reset_src(): if storage and "py_src" in storage: editor.setValue(storage["py_src"]) else: editor.setValue('import easy_plot as ep\n\n#ep.pie_chart()\n#ep.scatter_chart()\nep.line_chart()') editor.scrollToRow(0) editor.gotoLine(0) def reset_src_area(): if storage and "py_src" in storage: editor.value = storage["py_src"] else: editor.value = '# this is a demo for easy_chart in Brython\nimport easy_plot as ep\n\nep.scatter()\n' def write(data): doc["console"].value += str(data) #sys.stdout = object() #not needed when importing sys via src/Lib/sys.py sys.stdout.write = write #sys.stderr = object() # ditto sys.stderr.write = write def to_str(xx): return str(xx) output = '' def show_console(): doc["console"].value = output doc["console"].cols = 60 def clear_console(): doc["console"].value='' # can load data from AJAX the first time this page is called # and set the data as a global variable in this script # # code in exec() can see variables defined earlier def run(): global output doc["console"].value='' src = editor.getValue() if storage: storage["py_src"]=src t0 = time.time() try: exec(src) except Exception as exc: traceback.print_exc() output = doc["console"].value print('' %(time.time()-t0)) # load a Python script def on_complete(req): editor.setValue(req.text) if has_ace: editor.scrollToRow(0) editor.gotoLine(0) def load(evt): _name=evt.target.value req = ajax() req.on_complete = on_complete req.open('GET',_name+'?foo=%s' % _rand,False) req.send() def change_theme(evt): _theme=evt.target.value editor.setTheme(_theme) if storage: storage["ace_theme"]=_theme def reset_theme(): if storage: if "ace_theme" in storage: editor.setTheme(storage["ace_theme"]) doc["ace_theme"].value=storage["ace_theme"] if has_ace: reset_src() reset_theme() else: reset_src_area()