Ejemplo n.º 1
0
	def __present__(self, fragment):
		super_pres = super(ProjectRoot, self).__present__(fragment)

		def on_merge_upload(event):
			def on_upload(event, name, fp):
				self._merge_from_file(name, fp)
				dialog.dialog.close_containing_dialog(event)

			def on_cancel(event):
				dialog.dialog.close_containing_dialog(event)

			chooser = file_chooser.upload_file_chooser(on_upload, on_cancel)

			dialog.dialog(Html('<h3>Choose file to merge</h3>', chooser)).show_on(event)


		def on_merge_download(event):
			def on_downloaded(event, name, fp):
				self._merge_from_file(name, fp)
				dialog.dialog.close_containing_dialog(event)

			def on_cancel(event):
				dialog.dialog.close_containing_dialog(event)

			chooser = file_chooser.fetch_from_web_file_chooser(on_downloaded, on_cancel)

			dialog.dialog(Html('<h3>Enter URL to download file to merge</h3>', chooser)).show_on(event)


		def _on_set_package_name(event, name):
			self.__set_python_package_name_no_incr_change(name)

		def _on_unload(event):
			subject = fragment.subject
			try:
				unload_modules_and_display_notification = subject.document.unload_modules_and_display_notification
			except AttributeError:
				print 'WARNING: Could not unload_all_imported_modules; method unavailable'
				raise
			else:
				unload_modules_and_display_notification(fragment)


		merge_upload_button = button.button('Merge from file (upload)', on_merge_upload)
		merge_download_button = button.button('Merge from web', on_merge_download)

		python_package_name = self.python_package_name
		python_package_name = python_package_name   if python_package_name is not None  else ''
		entry = text_entry.text_entry(python_package_name, on_edit=_on_set_package_name)

		unload_button = button.button('Unload', _on_unload)

		contents = [
			'<div class="larch_app_title_bar"><h1 class="page_title">Project</h1></div>',
			'<p class="project_control">Merge in contents from other file: ', merge_upload_button, ' ', merge_download_button, '</p>',
			'<p class="project_root_package_name">Root package name: ', entry, '<br><span class="notes_text">(this is the base name from which the contents of this project will be importable)</span></p>',
			'<p class="project_control">Unload modules imported from project (Esc - U): ', unload_button, '</p>',
			super_pres,
		]
		return Html(*contents).use_css(url="/static/larch/project.css")
Ejemplo n.º 2
0
    def __present__(self, fragment):
        def on_rename(event):
            self.__node.name = self.__name.static_value
            self.close()

        def on_cancel(event):
            self.close()

        return Html('<div class="tool_box">',
                    '<span class="gui_section_1">Rename</span><br>', '<table>',
                    '<tr><td><span class="gui_label">Name:</span></td><td>',
                    text_entry.live_text_entry(self.__name), '</td></tr>',
                    '<tr><td>', button.button('Cancel',
                                              on_cancel), '</td><td>',
                    button.button('Rename', on_rename), '</td></tr>',
                    '</table>', '</div>')
Ejemplo n.º 3
0
    def __present__(self, fragment):
        def on_create(event):
            self.__container.append(
                self.__node_create_fn(self.__name.static_value))
            self.close()

        def on_cancel(event):
            self.close()

        return Html(
            '<div class="tool_box">',
            '<span class="gui_section_1">Create {0}</span><br>'.format(
                self.__node_type_name), '<table>',
            '<tr><td><span class="gui_label">Name:</span></td><td>',
            text_entry.live_text_entry(self.__name), '</td></tr>', '<tr><td>',
            button.button('Cancel', on_cancel), '</td><td>',
            button.button('Create',
                          on_create), '</td></tr>', '</table>', '</div>')
Ejemplo n.º 4
0
	def __doc_table_row(self, doc, page):
		def on_save(event):
			doc.save_and_display_notification(page)

		save_button = button.button('Save', on_save)
		doc_title = '<a href="/pages/{0}/{1}" class="larch_app_doc_title">{2}</a>'.format(self.loc_prefix, doc.location, doc.name)
		doc_filename = '<span class="larch_app_doc_filename">{0}</span><span class="larch_app_doc_extension">.ularch</span>'.format(doc.filename)
		controls = Html('<div class="larch_app_doc_controls">', save_button, '</div>')
		return Html('<tr class="larch_app_doc">	<td>', doc_title, '</td><td>', doc_filename, '</td><td>', controls, '</td></tr>')
Ejemplo n.º 5
0
	def __present__(self, fragment):
		def _on_reload(event):
			self.__user_docs.reload()
			if self.__docs is not None:
				self.__docs.reload()


		reset_button = button.button('Reload', _on_reload)
		reset_section = Html('<div>', reset_button, '</div>')

		add_notebook = menu.item('Notebook', lambda event: self.__tools.add(NewDocumentTool(self.__user_docs, notebook.Notebook, 'Notebook')))
		add_project = menu.item('Project', lambda event: self.__tools.add(NewDocumentTool(self.__user_docs, project_root.ProjectRoot, 'Project')))
		new_item = menu.sub_menu('New', [add_notebook, add_project])
		new_document_menu = menu.menu([new_item], drop_down=True)


		upload_ipynb = menu.item('Upload', lambda event: self.__tools.add(UploadIPynbTool(self.__user_docs)))
		web_ipynb = menu.item('Download from web', lambda event: self.__tools.add(DownloadIPynbFromWebTool(self.__user_docs)))
		import_ipynb_item = menu.sub_menu('Import IPython notebook', [upload_ipynb, web_ipynb])
		import_ipynb_menu = menu.menu([import_ipynb_item], drop_down=True)


		document_controls = Html('<table class="larch_app_controls_row"><tr><td class="larch_app_control">', new_document_menu, '</td>',
					 '<td class="larch_app_control">', import_ipynb_menu, '</td></tr></table>')


		contents = ["""
			<div class="larch_app_title_bar">The Ubiquitous Larch</div>

			<div class="larch_app_enclosure">
				<section class="larch_app_docs_section">
				<h2>Open documents:</h2>
			""",
			reset_section,
			self.__user_docs,
			document_controls,
			self.__tools,
			"""</section>
			<section>
			<p>For more information on using the Ubiquitous Larch, please see the <a href="/pages/docs/index">documentation</a>.</p>
			</section>
			<p class="larch_app_powered_by">The Ubiquitous Larch &copy; copyright Geoffrey French<br/>
			Powered by
			<a class="larch_app_pwr_link" href="http://www.python.org">Python</a>,
			<a class="larch_app_pwr_link" href="http://flask.pocoo.org">Flask</a>/<a class="larch_app_pwr_link" href="http://bottlepy.org">Bottle</a>/<a class="larch_app_pwr_link" href="http://www.djangoproject.com/">Django</a>,
			<a class="larch_app_pwr_link" href="http://jquery.com/">jQuery</a>,
			<a class="larch_app_pwr_link" href="http://www.json.org/js.html">json.js</a>,
			<a class="larch_app_pwr_link" href="http://codemirror.net/">Code Mirror</a>,
			<a class="larch_app_pwr_link" href="http://ckeditor.com/">ckEditor</a>,
			<a class="larch_app_pwr_link" href="http://d3js.org/">d3.js</a>,
			<a class="larch_app_pwr_link" href="http://imakewebthings.com/deck.js/">deck.js</a> and
			<a class="larch_app_pwr_link" href="http://needim.github.io/noty/">noty</a></p>
			</div>
			"""]
		return Html(*contents).use_css(url=larch_app_css)
Ejemplo n.º 6
0
	def __present__(self, fragment):
		def on_edit(text):
			self.__name = text

		def on_create(event):
			filename = _sanitise_filename(self.__name.static_value)
			if self.__doc_list.doc_for_filename(filename) is not None:
				self._tool_list.add(DocNameInUseTool(filename))
			else:
				self.__doc_list.new_document_for_content(self.__name.static_value, self.__document_factory)
			self.close()

		def on_cancel(event):
			self.close()

		return Html('<div class="tool_box">',
				'<span class="gui_section_1">Create document</span><br>',
				'<table>',
				'<tr><td><span class="gui_label">Name:</span></td><td>', text_entry.live_text_entry(self.__name, width="40em"), '</td></tr>',
				'<tr><td>', button.button('Cancel', on_cancel), '</td><td>', button.button('Create', on_create), '</td></tr>',
				'</table>',
				'</div>')
Ejemplo n.º 7
0
    def __present__(self, fragment):
        def on_move(destination):
            self.__node.parent.remove(self.__node)
            destination.append(self.__node)
            self.close()

        def on_cancel(event):
            self.close()

        return Html('<div class="tool_box">',
                    '<span class="gui_section_1">Move to:</span><br>',
                    '<ul class="project_move_dest_list">',
                    self.__container_dest(self.__root, on_move), '</ul>',
                    button.button('Cancel', on_cancel), '</div>')
Ejemplo n.º 8
0
def fetch_from_web_file_chooser(on_downloaded, on_cancel, user_agent=None):
    """
	Create a download form with an input box for a url

	:param on_downloaded: a callback that is invoked when the user chooses a file. It is of the form function(event, name, fp) where event is the triggering event, name is the file name and fp is a file like object
	:param on_cancel: a callback that is invoked when the user clicks the cancel button. Form: function(event)
	:param user_agent: the user agent to pass to the server
	"""
    if user_agent is None:
        user_agent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.13 (KHTML, like Gecko) Chrome/24.0.1284.0 Safari/537.13'
    url_live = LiveValue('')

    def on_fetch(event):
        url = url_live.static_value
        url_l = url.lower()
        if not url_l.startswith('http://') and not url_l.startswith(
                'https://'):
            url = 'http://' + url
        request = urllib2.Request(url)
        request.add_header('User-Agent', user_agent)
        opener = urllib2.build_opener()
        fp = opener.open(request)

        r = urlparse.urlparse(url)
        name = r.path.split('/')[-1]

        on_downloaded(event, name, fp)

    def _on_cancel(event):
        on_cancel(event)

    return Html('<div><span class="gui_label">URL: </span>',
                text_entry.live_text_entry(url_live, width="40em"),
                '</div>', '<table>', '<tr><td>',
                button.button('Cancel', _on_cancel), '</td><td>',
                button.button('Fetch',
                              on_fetch), '</td></tr>', '</table>', '</div>')
Ejemplo n.º 9
0
    def __present__(self, fragment):
        def on_execute(event):
            self.__console._execute_current_block(self)

        def on_execute_key(event, key):
            on_execute(event)
            return True

        code_area = Html('<div>', self.__code, '</div>')
        execute_button = button.button('Execute', on_execute)

        code_area_with_key_handler = code_area.with_key_handler(
            [KeyAction(KeyAction.KEY_DOWN, 13, ctrl=True)], on_execute_key)

        return Html('<div>', code_area_with_key_handler, execute_button,
                    '</div>')
Ejemplo n.º 10
0
def upload_file_chooser(on_choose, on_cancel):
    """
	Create an upload form

	:param on_choose: a callback that is invoked when the user chooses a file. It is of the form function(event, name, fp) where event is the triggering event, name is the file name and fp is a file like object
	:param on_cancel: a callback that is invoked when the user clicks the cancel button. Form: function(event)
	"""
    def _on_upload(event):
        f = event.data.get('file')
        if f is not None:
            on_choose(event, f.upload_name, f.file)
        else:
            on_cancel(event)

    def _on_cancel(event):
        on_cancel(event)

    upload_form_contents = Html(
        '<div><input type="file" name="file" size="50"/></div>', '<table>',
        '<tr><td>', button.button('Cancel', _on_cancel), '</td><td>',
        form.submit_button('Upload'), '</td></tr>', '</table>')
    upload_form = form.form(upload_form_contents, _on_upload)

    return upload_form
Ejemplo n.º 11
0
	def __menu_bar_contents__(self, page, fragment):
		#
		# File menu
		#

		def on_save():
			self._save_containing_document_and_display_notification(fragment)

		def on_unload():
			self._unload_from_containing_document_and_display_notification(fragment)


		save_item = menu.item('Save (Ctrl+S)', lambda event: on_save())
		unload_modules_item = menu.item('Unload modules', lambda event: on_unload())
		file_menu_contents = menu.sub_menu('File', [save_item, menu.separator(), unload_modules_item])
		file_menu = menu.menu([file_menu_contents], drop_down=True)



		#
		# Edit menu
		#

		def _insert_rich_text(below):
			self._insert_block(NotebookBlockText(self), below, self.__focused_block(fragment.page))

		def _insert_code(below):
			self._insert_block(NotebookBlockCode(self), below, self.__focused_block(fragment.page))

		def _insert_js(below):
			self._insert_block(NotebookBlockSource(self, 'js', 'js'), below, self.__focused_block(fragment.page))

		def _insert_css(below):
			self._insert_block(NotebookBlockSource(self, 'css', 'css'), below, self.__focused_block(fragment.page))

		def _insert_glsl(below):
			self._insert_block(NotebookBlockSource(self, 'glsl', 'glsl'), below, self.__focused_block(fragment.page))

		def _insert_html(below):
			self._insert_block(NotebookBlockSource(self, 'html', 'html'), below, self.__focused_block(fragment.page))

		insert_rich_text_above = menu.item('Insert rich text above', lambda event: _insert_rich_text(False))
		insert_code_above = menu.item('Insert executable Python code above', lambda event: _insert_code(False))
		insert_js_above = menu.item('Insert JS source above', lambda event: _insert_js(False))
		insert_css_above = menu.item('Insert CSS source above', lambda event: _insert_css(False))
		insert_html_above = menu.item('Insert HTML source above', lambda event: _insert_html(False))
		insert_glsl_above = menu.item('Insert GLSL source above', lambda event: _insert_glsl(False))

		insert_rich_text_below = menu.item('Insert rich text below', lambda event: _insert_rich_text(True))
		insert_code_below = menu.item('Insert executable Python code below', lambda event: _insert_code(True))
		insert_js_below = menu.item('Insert JS source below', lambda event: _insert_js(True))
		insert_css_below = menu.item('Insert CSS source below', lambda event: _insert_css(True))
		insert_html_below = menu.item('Insert HTML source below', lambda event: _insert_html(True))
		insert_glsl_below = menu.item('Insert GLSL source below', lambda event: _insert_glsl(True))

		remove_block = menu.item('Remove block', lambda event: self._delete_block(self.__focused_block(fragment.page)))
		edit_menu_contents = menu.sub_menu('Edit', [
			insert_rich_text_above,
			insert_code_above,
			insert_js_above,
			insert_css_above,
			insert_html_above,
			insert_glsl_above,

			menu.separator(),

			insert_rich_text_below,
			insert_code_below,
			insert_js_below,
			insert_css_below,
			insert_html_below,
			insert_glsl_below,

			menu.separator(),

			remove_block])

		edit_menu = menu.menu([edit_menu_contents], drop_down=True)

		exec_button = button.button('Execute (Ctrl-Enter)', lambda event: self.execute())

		return [file_menu, edit_menu, exec_button, self.__execution_state]
Ejemplo n.º 12
0
	def __present__(self, fragment):
		return Html('<div class="larch_app_doc_name_in_use"><p class="error_text">There is already a document in a file named \'{0}<em>.ularch</em>\'.</p>'.format(self.filename),
			    button.button('Close', lambda event: self.close), '</div>')