diff --git a/book/_static/element_pdf_and_cdf.html b/book/_static/element_pdf_and_cdf.html new file mode 100644 index 0000000..d41fc47 --- /dev/null +++ b/book/_static/element_pdf_and_cdf.html @@ -0,0 +1,392 @@ + + + + + + + + + +
+ + +
+ + + + \ No newline at end of file diff --git a/book/_toc.yml b/book/_toc.yml index 693e255..e3e1240 100644 --- a/book/_toc.yml +++ b/book/_toc.yml @@ -39,7 +39,8 @@ parts: sections: - file: features/grasple.md - file: features/h5p.md - - file: features/live_code.ipynb + - file: features/adding_basic_HTML_elements.md + - file: features/live_code.md sections: - file: features/widgets.ipynb - file: features/exercise_checking.ipynb diff --git a/book/features/adding_basic_HTML_elements.md b/book/features/adding_basic_HTML_elements.md new file mode 100644 index 0000000..113e19a --- /dev/null +++ b/book/features/adding_basic_HTML_elements.md @@ -0,0 +1,863 @@ +# Adding basic interactive HTML/JavaScript elements + +## Embedding HTML files +A straightforward way to create interactive web-based elements is to code them directly using HTML with JavaScript. This allows us to build elements such as the one below, which shows the connection between a probability density function (pdf) and the associated cumulative density function (cdf): + + + +If we want to embed such figures in the teachbook, you can do so via an iframe which reference a html file in the _static folder: + +```html + +``` + +Here, the src path moves up one subdirectory, then enters the _static folder, and loads the element_pdf_and_cdf.html file. So how do we create this figure? + +## Creating a basic interactive HTML/JavaScript element + +At the bottom of this page, I will provide the full code for the interactive element above. In this section, let us go through the code piece by piece. Let us begin by looking at the start of the code. + +We begin by specifying that this file is a HTML file. In the header, we can load packages we need later on. Within the ` + + + + +``` + +Next, we create a `
`, an HTML element that can group other HTML elements, that contians a [SVG](https://en.wikipedia.org/wiki/SVG) named `"click"`. This SVG will serve as the canvas on which we will place the interactive content. + +```html + +
+ + +
+``` + +So far, all of this has just been setup. The real magic begins in the ` + + +``` + +## HTML code for the pdf/cdf element + +This is the code for the element above: + +```html + + + + + + + + + +
+ + +
+ + + + + + +``` \ No newline at end of file diff --git a/book/features/interactive_element.ipynb b/book/features/interactive_element.ipynb index f0099c0..b29afd2 100644 --- a/book/features/interactive_element.ipynb +++ b/book/features/interactive_element.ipynb @@ -15,7 +15,9 @@ " - JupyterQuiz: code-based implementation of multiple-choiche questions and numerical answer questions. This is a basic open course tool which requires you to write your quiz questions in Python language as a `dict` or to a `.json` file.\n", " - H5p: TU-Delft-licensed tool to create questions (and many other interactive elements) using an [online GUI](https://tudelft.h5p.com/). These elements are embedded into the book using an iframe which requires very little coding. This elements are easier to implement than the JupyterQuiz and have more features (although proper numerical answer questions are missing). More information on this is provided in [this subchapter](h5p.md)\n", " - Grasple-exercises: TU-Delft-licensed tool to creating math-based quiz questions. Again, these elements are embedded into the book using an iframe which requiries very little coding. These elements are very well suited to math-based problems as it allows analytical evaluation of numerical values and formulas. Furthermore, Grasple allows parameterization (not required for formative exercises). More information on this is provided in [this subchapter](grasple.md)\n", - " - Non-kernel-based widgets: You can create interactive widgets with Python which doesn't need a kernel to run online. The use of the [plotly](https://plotly.com/python/getting-started/) package is an example which allows this use.\n", + " - Non-kernel-based widgets: You can create interactive widgets with Python which doesn't need a kernel to run online:\n", + " - You can create html/javascript widgets, an example is shown in [this subchapter](adding_basic_HTML_elements.md)\n", + " - The use of the [plotly](https://plotly.com/python/getting-started/) package is an example which allows this use.\n", "\n", "Examples of each type is shown below based on the following problem:\n", "\n", diff --git a/book/features/live_code.md b/book/features/live_code.md new file mode 100644 index 0000000..a431e7a --- /dev/null +++ b/book/features/live_code.md @@ -0,0 +1,90 @@ +# Python-enabled interactivity + +Our book has been enable to run Python code live in the browser (thanks Max!). This page contains some installation instructions and the other sections show how to use this functionality to create interactive figures and feedback on code + +## Setting up Python live coding +To set up the Python live coding you need to add our [own sphinx-thebe extension](https://gitlab.tudelft.nl/interactivetextbooks-citg/extensions/sphinx-thebe) to your book. This extensions doesn't rely on a 3rd party like Binder and it supports local python execution and custom features. Therefore, you need to add some lines to `requirements.txt` and `_config.yml` + +For `requirements.txt` add the following lines: +``` +--extra-index-url https://gitlab.tudelft.nl/api/v4/projects/11239/packages/pypi/simple +sphinx-thebe ~= 0.9.9 +``` +This will download the correct version of the sphinx extension when the book is build on the server (which loads the required packages from `requirements.txt`) + +Afterwards, this sphinx extension needs to be enabled in your book. This can be done by adding the following lines to `_config.yml`: + +``` +launch_buttons: + thebe: true + +sphinx: + config: + html_js_files: + - https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js + thebe_config: + use_thebe_lite: true + exclude_patterns: ["**/_*.yml", "**/*.md", "**/*.ipynb"] +``` + +The `html_js_files` link calls for some required javascript files. `use_thebe_lite` makes sure you initiate our adapted sphinx thebe extensions. The `exclude_patterns` makes sure you import all files so that they can be accessed from your code, except for the files matching the patterns shown. By `launch_buttons` you initiate the {fa}`rocket` --> {guilabel}`Live Code` on the top right corner of every page generated from a `.ipynb` file. + +Note, if you're messing around with `html_theme_options` (for example when adding more buttons), all button behaviour is affected. In that case, as opposed to the jupyter book documentation, all buttons have to be specified from within `sphinx: html_theme_options` where you might also specify other buttons as well (ie. the [code repository button](https://jupyterbook.org/en/stable/basics/repository.html)). + +``` +sphinx: + config: + html_js_files: + - https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js + thebe_config: + use_thebe_lite: true + exclude_patterns: ["**/_*.yml", "**/*.md", "**/*.ipynb"] + html_theme_options: + repository_url: "hello!" + use_repository_button: true + launch_buttons: + thebe: true +``` + +## Instructions: local build + +To test the fuctionality, you have to run a local server, otherwise the interactivity doens't work. So, you cannot just open the `index.html` file any more. This can be done with the following steps: +1. build the book by running ` jupyter book build `. If you are using Windows you should use the Git Bash terminal. Set this as your default in VS Code, then open it using "New Terminal." If you have not already done so, you will need to allow Git Bash to use your Python libraries by running the command `conda init bash`. +2. Set up a server by running `python -m http.server 8000`. +3. Open the book by going to [http://localhost:8000/](http://localhost:8000/) in your browser. You may need to reload the page once the rebuild is complete. You can do this on Chrome by right-clicking somewhere on the page, select "Inspect", open the "Network" tab, then reload with `CTRL+R`. Or try `ctrl`+`F5` +4. Go to any page that is created from a Jupyter notebook + +If you push to main, you can test the interactivity on the book-draft website as well. + +## Custom cell tags +With the extended functionality of live code, additional cell tags have been developed to be added to cells in any .ipynb file, next to the [existing tags] of the jupyter book (https://jupyterbook.org/en/stable/content/metadata.html): +- `disable-execution-cell` disables the ability to execute the cell when thebe is activated. This might be useful if you notebook file includes interactivity for only a part of the coding cells +- `disable-execution-page` disables the ability to execute the entire page. This might be useful if you use python code for creating a page, but the code is not part of the actual content of the book. +- `auto-execute-page` automatically starts the thebe live coding functionality whenever the page is opened. This is particularly useful if you're using widgets on a page. +- `thebe-init` directly starts running this cell as soon as the thebe live coding functionality starts. +- `thebe-remove-input-init`, as `thebe-init`, starts the cell directly as soon as the thebe live coding functionality starts. However, the input is not shown to the students (in static and interactive mode). The original `remove-input` tag will not work as it deletes the entire cell, so it can never be executed. + +## Additional packages + +The python kernel doesn't have all packages standard included. Some of the most used packages which are included are: +- `python` +- `numpy` +- `scipy` +- `sympy` +- `matplotlib` +- `ipywidgets` + +If you'd like to install more package, you can do so by added a codecell (preferably hidden using `thebe-remove-input-init`) with the following command: + +```python +import micropip +await micropip.install("package_name") +``` + +This will install the packages: Micropip will look at the Pyodide package index, but also at the general PyPi index. If a package is pure python (i.e. no C extensions), then it can also be used by Pyodide. + +If the packages are not included in Pyodide, you can use `pip: + +```python +%pip install package_name +```