Commit 49489588 by Sven Marnach

Add skeleton created by the XBlock SDK.

parent e5487a6b
__pycache__/
*.py[cod]
from .activetable import ActiveTableXBlock
"""TO-DO: Write a description of what this XBlock is."""
import pkg_resources
from xblock.core import XBlock
from xblock.fields import Scope, Integer
from xblock.fragment import Fragment
class ActiveTableXBlock(XBlock):
"""
TO-DO: document what your XBlock does.
"""
# Fields are defined on the class. You can access them in your code as
# self.<fieldname>.
# TO-DO: delete count, and define your own fields.
count = Integer(
default=0, scope=Scope.user_state,
help="A simple counter, to show something happening",
)
def resource_string(self, path):
"""Handy helper for getting resources from our kit."""
data = pkg_resources.resource_string(__name__, path)
return data.decode("utf8")
# TO-DO: change this view to display your data your own way.
def student_view(self, context=None):
"""
The primary view of the ActiveTableXBlock, shown to students
when viewing courses.
"""
html = self.resource_string("static/html/activetable.html")
frag = Fragment(html.format(self=self))
frag.add_css(self.resource_string("static/css/activetable.css"))
frag.add_javascript(self.resource_string("static/js/src/activetable.js"))
frag.initialize_js('ActiveTableXBlock')
return frag
# TO-DO: change this handler to perform your own actions. You may need more
# than one handler, or you may not need any handlers at all.
@XBlock.json_handler
def increment_count(self, data, suffix=''):
"""
An example handler, which increments the data.
"""
# Just to show data coming in...
assert data['hello'] == 'world'
self.count += 1
return {"count": self.count}
# TO-DO: change this to create the scenarios you'd like to see in the
# workbench while developing your XBlock.
@staticmethod
def workbench_scenarios():
"""A canned scenario for display in the workbench."""
return [
("ActiveTableXBlock",
"""<vertical_demo>
<activetable/>
<activetable/>
<activetable/>
</vertical_demo>
"""),
]
This static directory is for files that should be included in your kit as plain
static files.
You can ask the runtime for a URL that will retrieve these files with:
url = self.runtime.local_resource_url(self, "static/js/lib.js")
The default implementation is very strict though, and will not serve files from
the static directory. It will serve files from a directory named "public".
Create a directory alongside this one named "public", and put files there.
Then you can get a url with code like this:
url = self.runtime.local_resource_url(self, "public/js/lib.js")
The sample code includes a function you can use to read the content of files
in the static directory, like this:
frag.add_javascript(self.resource_string("static/js/my_block.js"))
/* CSS for ActiveTableXBlock */
.activetable_block .count {
font-weight: bold;
}
.activetable_block p {
cursor: pointer;
}
<div class="activetable_block">
<p>ActiveTableXBlock: count is now
<span class='count'>{self.count}</span> (click me to increment).
</p>
</div>
/* Javascript for ActiveTableXBlock. */
function ActiveTableXBlock(runtime, element) {
function updateCount(result) {
$('.count', element).text(result.count);
}
var handlerUrl = runtime.handlerUrl(element, 'increment_count');
$('p', element).click(function(eventObject) {
$.ajax({
type: "POST",
url: handlerUrl,
data: JSON.stringify({"hello": "world"}),
success: updateCount
});
});
$(function ($) {
/* Here's where you'd do things on page load. */
});
}
"""Setup for activetable XBlock."""
import os
from setuptools import setup
def package_data(pkg, roots):
"""Generic function to find package_data.
All of the files under each of the `roots` will be declared as package
data for package `pkg`.
"""
data = []
for root in roots:
for dirname, _, files in os.walk(os.path.join(pkg, root)):
for fname in files:
data.append(os.path.relpath(os.path.join(dirname, fname), pkg))
return {pkg: data}
setup(
name='activetable-xblock',
version='0.1',
description='activetable XBlock', # TODO: write a better description.
packages=[
'activetable',
],
install_requires=[
'XBlock',
],
entry_points={
'xblock.v1': [
'activetable = activetable:ActiveTableXBlock',
]
},
package_data=package_data("activetable", ["static", "public"]),
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment