def __init__(self, notebook_dir=None, template_dir=None): super(NotebookManagerView, self).__init__() if notebook_dir is None: notebook_dir = os.getcwd() self.notebook_dir = path(notebook_dir).abspath() self.template_dir = template_dir self.notebook_manager = SessionManager(daemon=True)
class NotebookManagerView(SlaveView): def __init__(self, notebook_dir=None): super(NotebookManagerView, self).__init__() if notebook_dir is None: notebook_dir = os.getcwd() self.notebook_dir = path(notebook_dir).abspath() self.notebook_manager = SessionManager() def create_ui(self): box = Gtk.Box(spacing=6) box.set_orientation(Gtk.Orientation.HORIZONTAL) new_button = Gtk.Button('New...') open_button = Gtk.Button('Open...') new_button.connect('clicked', self.on_new) open_button.connect('clicked', self.on_open) new_button.show() open_button.show() box.pack_end(new_button, False, False, 0) box.pack_end(open_button, False, False, 0) self.widget.pack_start(box, False, False, 0) self.parent = None parent = self.widget.get_parent() while parent is not None: self.parent = parent parent = parent.get_parent() def on_open(self, button): buttons = (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK) dialog = Gtk.FileChooserDialog("Open notebook", self.parent, Gtk.FileChooserAction.OPEN, buttons) add_filters(dialog, [{ 'name': 'IPython notebook (*.ipynb)', 'pattern': '*.ipynb' }]) response = dialog.run() if response == Gtk.ResponseType.OK: selected_path = dialog.get_filename() self.notebook_manager.open(selected_path) dialog.destroy() def on_new(self, button): ''' Copy selected notebook template to notebook directory. ## Notes ## - An exception is raised if the parent of the selected file is the notebook directory. - If notebook with same name already exists in notebook directory, offer is made to overwrite (the new copy of the file is renamed with a count if overwrite is not selected). ''' buttons = (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK) dialog = Gtk.FileChooserDialog("Select notebook template", self.parent, Gtk.FileChooserAction.OPEN, buttons) add_filters(dialog, [{ 'name': 'IPython notebook (*.ipynb)', 'pattern': '*.ipynb' }]) response = dialog.run() if response == Gtk.ResponseType.OK: selected_path = path(dialog.get_filename()) output_path = self.notebook_dir.joinpath(selected_path.name) overwrite = False if output_path.isfile(): response = yesno( '%s already exists. Overwrite?' % output_path.name, 'Overwrite?') if response == Gtk.ResponseType.YES: overwrite = True else: counter = 1 renamed_path = output_path while renamed_path.isfile(): new_name = '%s (%d)%s' % (output_path.namebase, counter, output_path.ext) renamed_path = output_path.parent.joinpath(new_name) counter += 1 output_path = renamed_path self.notebook_manager.launch_from_template( selected_path, overwrite=overwrite, output_name=output_path.name, notebook_dir=self.notebook_dir) dialog.destroy()
def __init__(self, notebook_dir=None): super(NotebookManagerView, self).__init__() if notebook_dir is None: notebook_dir = os.getcwd() self.notebook_dir = path(notebook_dir).abspath() self.notebook_manager = SessionManager()
class NotebookManagerView(SlaveView): def __init__(self, notebook_dir=None, template_dir=None): super(NotebookManagerView, self).__init__() if notebook_dir is None: notebook_dir = os.getcwd() self.notebook_dir = path(notebook_dir).abspath() self.template_dir = template_dir self.notebook_manager = SessionManager(daemon=True) def sessions_dialog(self): session_list = NotebookManagerList(self.notebook_manager) dialog = gtk.Dialog(title='Notebook session manager', parent=self.parent, flags=gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK)) dialog.set_transient_for(self.parent) dialog.get_content_area().pack_start(session_list.widget) return dialog def create_ui(self): box = gtk.HBox() new_button = gtk.Button('New...') open_button = gtk.Button('Open...') manager_button = gtk.Button('Manage sessions...') new_button.connect('clicked', self.on_new) open_button.connect('clicked', self.on_open) manager_button.connect('clicked', self.on_manager) box.pack_end(new_button, False, False, 0) box.pack_end(open_button, False, False, 0) box.pack_end(manager_button, False, False, 0) self.widget.pack_start(box, False, False, 0) self.parent = None parent = self.widget.get_parent() while parent is not None: self.parent = parent parent = parent.get_parent() self.widget.show_all() def get_parent(self): self.parent = None parent = self.widget.get_parent() while parent is not None: self.parent = parent parent = parent.get_parent() return self.parent def on_manager(self, button): parent = self.get_parent() dialog = self.sessions_dialog() dialog.show_all() if parent is not None: parent.set_sensitive(False) dialog.run() dialog.destroy() if parent is not None: parent.set_sensitive(True) def on_open(self, button): buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK) dialog = gtk.FileChooserDialog("Open notebook", self.parent, gtk.FILE_CHOOSER_ACTION_OPEN, buttons) add_filters(dialog, [{'name': 'IPython notebook (*.ipynb)', 'pattern': '*.ipynb'}]) dialog.set_current_folder(self.notebook_dir) response = dialog.run() if response == gtk.RESPONSE_OK: selected_path = dialog.get_filename() self.notebook_manager.open(selected_path) dialog.destroy() def on_new(self, button): ''' Copy selected notebook template to notebook directory. ## Notes ## - An exception is raised if the parent of the selected file is the notebook directory. - If notebook with same name already exists in notebook directory, offer is made to overwrite (the new copy of the file is renamed with a count if overwrite is not selected). ''' buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK) dialog = gtk.FileChooserDialog("Select notebook template", self.parent, gtk.FILE_CHOOSER_ACTION_OPEN, buttons) add_filters(dialog, [{'name': 'IPython notebook (*.ipynb)', 'pattern': '*.ipynb'}]) if self.template_dir is not None: dialog.set_current_folder(self.template_dir) response = dialog.run() if response == gtk.RESPONSE_OK: selected_path = path(dialog.get_filename()) output_path = self.notebook_dir.joinpath(selected_path.name) overwrite = False if output_path.isfile(): response = yesno('%s already exists. Overwrite?' % output_path.name) if response == gtk.RESPONSE_YES: overwrite = True else: counter = 1 renamed_path = output_path while renamed_path.isfile(): new_name = '%s (%d)%s' % (output_path.namebase, counter, output_path.ext) renamed_path = output_path.parent.joinpath(new_name) counter += 1 output_path = renamed_path self.notebook_manager.launch_from_template(selected_path, overwrite=overwrite, output_name=output_path.name, notebook_dir=self.notebook_dir) dialog.destroy() def stop(self): self.notebook_manager.stop() def __del__(self): self.stop()