def run_code(self, code, outputfile): """ Run shoebot code, sets random.seed to stabilize output. """ bot = create_bot(outputfile=outputfile) seed(0) bot.run(code)
def run_script(self, callback_action, widget): # get the buffer contents buffer = self.text_view.get_buffer() start, end = buffer.get_bounds() codestring = buffer.get_text(start, end) try: if buffer.filename: os.chdir(os.path.dirname(buffer.filename)) bot = shoebot.create_bot(codestring, 'NodeBox', server=self.use_socketserver, show_vars=self.use_varwindow, window = True) self.sbot_window = bot._canvas.sink bot.run(codestring, run_forever = True, iterations = None) except ShoebotError, NameError: import traceback import sys errmsg = traceback.format_exc(limit=1) err = "Error in Shoebot script:\n %s" % (errmsg) dialog = Gtk.MessageDialog(self, Gtk.DialogFlags.MODAL, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, err); result = dialog.run() dialog.destroy() return False
def run_script(self, widget): # get the buffer contents buffer = self.text_view.get_buffer() start, end = buffer.get_bounds() codestring = buffer.get_text(start, end, include_hidden_chars=False) try: if buffer.filename: os.chdir(os.path.dirname(buffer.filename)) bot = shoebot.create_bot(codestring, 'NodeBox', server=self.use_socketserver, show_vars=self.use_varwindow, window=True) self.sbot_window = bot._canvas.sink bot.run(codestring, run_forever=True, iterations=None, frame_limiter=True) except ShoebotError, NameError: import traceback import sys errmsg = traceback.format_exc(limit=1) err = "Error in Shoebot script:\n %s" % (errmsg) dialog = Gtk.MessageDialog(self, Gtk.DialogFlags.MODAL, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, err) result = dialog.run() dialog.destroy() return False
def on_run_script(self, widget): # get the buffer contents buffer = self.text_view.get_buffer() start, end = buffer.get_bounds() codestring = buffer.get_text(start, end, include_hidden_chars=False) try: buffer_dir = os.path.dirname(buffer.filename or '') if buffer_dir: os.chdir(buffer_dir) bot = shoebot.create_bot(codestring, shoebot.NODEBOX, server=self.use_socketserver, show_vars=self.use_varwindow, title=self.window_title, window=True) self.sbot_window = bot._canvas.sink bot.run(codestring, run_forever=True, iterations=None, frame_limiter=True) except ShoebotError, NameError: import traceback import sys errmsg = traceback.format_exc(limit=1) err = "Error in Shoebot script:\n %s" % (errmsg) dialog = Gtk.MessageDialog(self, Gtk.DialogFlags.MODAL, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, err) dialog.run() dialog.destroy() self.sbot_window = None return False
def run_code(self, code, outputfile, windowed=False, namespace=None): """ Run shoebot code, sets random.seed to stabilize output. """ bot = create_bot(window=windowed, outputfile=outputfile, namespace=namespace) seed(0) bot.run(code)
def run(): print("shoebot - %s:" % f.__name__.replace("_", " ")) try: import shoebot outputfile="/tmp/shoebot-%s.png" % f.__name__ bot = shoebot.create_bot(outputfile=outputfile) f(bot) bot.finish() print("[passed] : %s" % outputfile) except Exception as e: exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exc() print("[failed]")
def paintWorld(world, code, side=900, num=False): bot = shoebot.create_bot(outputfile="world1990-11.pdf") bot.color_mode = "hsb" sideStep = int(math.sqrt(len(world))) + 1 step = int(side / sideStep) canvas_width = step * sideStep canvas_height = step * sideStep bot.size(canvas_width + 2, canvas_height + 2) bot.stroke(0) bot.fill(1) #bot.rect(0, 0, canvas_width, canvas_height) x = 0 y = 0 print(len(world)) i = 0 for triple in world: tr = assembleTypeFromList(triple) bot.fill(1) bot.stroke(0) bot.rect(x, y, step, step) bot.fill(0) if num == True: bot.text(str(i), x + 5, y + 20) j = 0 for sf in code: if re.search(sf, tr): # painting: e.g. 8 sf, then 8 potential squares # inside one other, each with 1 of 8 hues #print("matched: "+tr+" by "+sf) #col = (float(j)/float(len(code.keys())), 0.9, 0.9) #bot.fill(col) col = (float(j) / len(code.keys())) col = 1 - (col * 0.7 + 0.3) bot.fill(col) bot.stroke(col) base = float(len(code.keys())) + 3 mul = step * (base - (j)) / base bot.oval(x + step * 0.5 - mul * 0.5, y + step * 0.5 - mul * 0.5, mul, mul) # does sf e match the triplet i? # yes: set color j = j + 1 x = x + step if x >= (canvas_width): x = 0 y = y + step i = i + 1 bot.finish()
def do_execute(self, code, silent, store_history=True, user_expressions=None, allow_stdin=False): png_data = io.BytesIO() bot = shoebot.create_bot(buff=png_data, format="png") exc = None try: bot.run(code, break_on_error=True) # quote and encode PNG data for passing JSON response to Jupyter # https://ipython-books.github.io/16-creating-a-simple-kernel-for-jupyter/ png_string = base64.b64encode(png_data.getvalue()).decode("utf-8") except Exception as e: import traceback exc = traceback.format_exc(e) if not silent: if exc: stream_content = {'name': 'stdout', 'text': exc} self.send_response(self.iopub_socket, 'stream', stream_content) else: content = { 'source': 'kernel', 'data': { 'image/png': png_string }, 'metadata': { 'image/png': { 'width': bot.WIDTH, 'height': bot.HEIGHT } } } self.send_response(self.iopub_socket, 'display_data', content) return { 'status': 'ok', # The base class increments the execution count 'execution_count': self.execution_count, 'payload': [], 'user_expressions': {}, }
def run(): from shoebot import ShoebotInstallError # https://github.com/shoebot/shoebot/issues/206 print(" Shoebot - %s:" % f.__name__.replace("_", " ")) try: import shoebot outputfile = "/tmp/shoebot-%s.png" % f.__name__ bot = shoebot.create_bot(outputfile=outputfile) f(bot) bot.finish() print(' [passed] : %s' % outputfile) print('') except ShoebotInstallError as e: print(' [failed]', e.args[0]) print('') except Exception: print(' [failed] - traceback:') for line in traceback.format_exc().splitlines(): print(' %s' % line) print('')
def on_run_script(self, widget): # get the buffer contents source_buffer = self.source_view.get_buffer() start, end = source_buffer.get_bounds() codestring = source_buffer.get_text(start, end, include_hidden_chars=False) window_title = self.get_title() try: buffer_dir = os.path.dirname(source_buffer.filename or "") if buffer_dir: os.chdir(buffer_dir) bot = shoebot.create_bot( codestring, shoebot.NODEBOX, server=self.use_socketserver, show_vars=self.use_varwindow, title=window_title, window=True, ) self.shoebot_window = bot._canvas.sink bot.run(codestring, run_forever=True, iterations=None, frame_limiter=True) except (ShoebotError, NameError): import traceback import sys errmsg = traceback.format_exc(limit=1) err = _("Error in Shoebot script:") + "\n {errmsg}" dialog = Gtk.MessageDialog( self, Gtk.DialogFlags.MODAL, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, err, ) result = dialog.run() dialog.destroy() self.shoebot_window = None return False
def render_bot(filename, inline=False, format='svg', **kwargs): """ Run a named bot, and output SVG. :param inline: If True Output SVG with no XML preamble (suitable for mixing with HTML). """ if not bot_allowed(filename): raise Exception("Sorry this bot is not on the allowed list") buff = StringIO() bot = shoebot.create_bot(buff=buff, **kwargs) bot.run(filename) if inline: buff.seek(XML_PREAMBLE_LENGTH) else: buff.seek(0) return buff.read()
def run(): from shoebot.util import ShoebotInstallError print(" Shoebot - %s:" % f.__name__.replace("_", " ")) try: import shoebot outputfile = "/tmp/shoebot-%s.png" % f.__name__ bot = shoebot.create_bot(outputfile=outputfile) f(bot) bot.finish() print(" [passed] : %s" % outputfile) print("") except ShoebotInstallError as e: print(" [failed]", e.args[0]) print("") except Exception: print(" [failed] - traceback:") for line in traceback.format_exc().splitlines(): print(" %s" % line) print("")
import yaml import common as c import shoebot import sys inputfile = sys.argv[1] outputfile = sys.argv[2] print "Input file = {}".format(inputfile) print "Output file = {}".format(outputfile) with open(inputfile, "r") as f: data = yaml.load(f.read()) # 8.5x11" bot = shoebot.create_bot(outputfile=outputfile) bot.size(data['page']['width'], data['page']['height']) default_color = '#a74ed5' colors = { 'blue': '#156eb4', 'red': '#d61d67', 'purple': '#a74ed5', } bot.font("Minion", fontsize=11) bot.background(1) # Swap out [ending] for <ending>ending</ending> def style_line(l):
def run_shoebot_code(code, outputfile): bot = shoebot.create_bot(outputfile=outputfile) bot.run(code) return outputfile