def new_notebook(self, fn, aux=False): " Creates an empty notebook on disc " if os.path.exists(fn): return aux create_notebook_folder(fn) _save_notebook(fn, self.resource.new_notebook) if aux: if aux.get('type') in ('calendar'): nb = get_notebook(fn) nb['_metadata']['raw_date'] = aux.get('raw_date', '') _save_notebook(fn, nb) new_notebook(fn, self.resource)
def handler(self, message): code = message.get('code', '') exit_status = False pwd = os.getcwd() # TODO: this needs a bit of a clean-up... #if message['directory']: os.chdir(message['directory'].strip('<br>')) new_path = create_notebook_folder(message.get('file')) # TODO: This is a beauty plaster for the moment... os.chdir(new_path) fn = os.path.basename(message.get('filename')) out_file = os.path.join(new_path, fn + '.png') print out_file if code.startswith('#gnuplot') or code.startswith('# gnuplot'): with open(fn + '.gp', 'w') as fout: # We should make this configurable from the resource fout.write("set term png; set out '%s.png'\n"%(os.path.join(new_path, fn)) + code) if self.resource.plot_pdf_output: fout.write("\nset term pdfcairo; set out '%s.pdf'\n replot\n"%(os.path.join(new_path, fn))) os.system("gnuplot %s.gp"%(fn)) os.system("rm %s.gp -f"%(fn)) else: if not self.resource.has_matplotlib: exit_status = 'Could not import matplotlib. Choose gnuplot as the plotting back-end.' else: x = linspace(-10, 10, 100) try: exec(code) os.chdir(pwd) savefig(out_file) if self.resource.plot_pdf_output: savefig(out_file.replace('.png', '.pdf')) close() except: exit_status = traceback.format_exc().replace('\n', '<br>') os.chdir(pwd) if not exit_status: exit_status = self.read_plot(out_file) return {'success': 'success', 'out_file': out_file, 'body': exit_status}