Skip to content

Commit

Permalink
docs: Match docs on thumbs.py with the actual code in the xblock sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
sarina authored and feanil committed Dec 19, 2023
1 parent 83b2ee8 commit bb3f43d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
28 changes: 16 additions & 12 deletions docs/xblock-tutorial/anatomy/python.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.. _The XBlock Python File:

#######################
######################
The XBlock Python File
#######################
######################

This section of the tutorial walks through the Python file, `thumbs.py`_, for
the Thumbs XBlock example in the XBlock SDK.
Expand All @@ -18,9 +18,9 @@ scenarios.
:local:
:depth: 1

********************
*******************
Thumb XBlock Fields
********************
*******************

The ``thumbs.py`` file defines the following fields for the XBlock in the
``ThumbsBlockBase`` class.
Expand All @@ -42,9 +42,9 @@ Note the following details about the fields in the Thumbs XBlock.

For more information, see :ref:`XBlock Fields`.

**************************
*************************
Thumb XBlock Student View
**************************
*************************

The ``thumbs.py`` file defines the student view for the XBlock in the
``ThumbsBlockBase`` class.
Expand All @@ -60,24 +60,28 @@ Note the following details about student view.

.. code-block:: python
html_str = pkg_resources.resource_string(__name__, "static/html/thumbs.html")
frag = Fragment(unicode(html_str).format(self=self))
html_str = pkg_resources.resource_string(
__name__,
"static/html/thumbs.html".decode('utf-8')
)
frag = Fragment(str(html_str).format(block=self))
* The JavaScript and CSS file contents are added to the fragment with the
``add_javascript()`` and ``add_css`` methods.
``add_javascript()`` and ``add_css()`` methods.

* The JavaScript in the fragment must be initialized using the name of the
XBlock class. The name also maps to the function that initializes the XBlock in the :ref:`JavaScript file <The XBlock JavaScript File>`.
XBlock class. The name also maps to the function that initializes the XBlock
in the :ref:`JavaScript file <The XBlock JavaScript File>`.

.. code-block:: python
frag.initialize_js('ThumbsBlock')
For more information, see :ref:`View Methods`.

**************************
*************************
Thumb XBlock Vote Handler
**************************
*************************

The ``thumbs.py`` file defines a handler that adds a user's vote to the XBlock.

Expand Down
21 changes: 15 additions & 6 deletions docs/xblock-tutorial/reusable/code_thumbs_fields.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
.. code-block:: python
class ThumbsBlockBase(object):
upvotes = Integer(help="Number of up votes", default=0,
scope=Scope.user_state_summary)
downvotes = Integer(help="Number of down votes", default=0,
scope=Scope.user_state_summary)
voted = Boolean(help="Has this student voted?", default=False,
scope=Scope.user_state)
upvotes = Integer(
help="Number of up votes",
default=0,
scope=Scope.user_state_summary
)
downvotes = Integer(
help="Number of down votes",
default=0,
scope=Scope.user_state_summary
)
voted = Boolean(
help="Has this student voted?",
default=False,
scope=Scope.user_state
)
22 changes: 15 additions & 7 deletions docs/xblock-tutorial/reusable/code_thumbs_student_view.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,24 @@
# Load the HTML fragment from within the package and fill in the template
html_str = pkg_resources.resource_string(__name__, "static/html/thumbs.html")
frag = Fragment(unicode(html_str).format(self=self))
html_str = pkg_resources.resource_string(
__name__,
"static/html/thumbs.html".decode('utf-8')
)
frag = Fragment(str(html_str).format(block=self))
# Load the CSS and JavaScript fragments from within the package
css_str = pkg_resources.resource_string(__name__, "static/css/thumbs.css")
frag.add_css(unicode(css_str))
css_str = pkg_resources.resource_string(
__name__,
"static/css/thumbs.css".decode('utf-8')
)
frag.add_css(str(css_str))
js_str = pkg_resources.resource_string(__name__,
"static/js/src/thumbs.js")
frag.add_javascript(unicode(js_str))
js_str = pkg_resources.resource_string(
__name__,
"static/js/src/thumbs.js".decode('utf-8')
)
frag.add_javascript(str(js_str))
frag.initialize_js('ThumbsBlock')
return frag

0 comments on commit bb3f43d

Please sign in to comment.