diff --git a/course_utils/parsons_builder.py b/course_utils/parsons_builder.py index 72b8b17..f13f8f3 100644 --- a/course_utils/parsons_builder.py +++ b/course_utils/parsons_builder.py @@ -38,7 +38,9 @@ def load_yaml(yaml_files): yield None else: # Strips leading/trailing whitespace from the code block and split on line breaks - parsons_dict['python_code'] = parsons_dict['python_code'].strip().split('\n') + for key in ('python_code', 'python_setup'): + if key in parsons_dict: + parsons_dict[key] = parsons_dict[key].strip().split('\n') # Randomize code order shuffle(parsons_dict['python_code']) yield file, parsons_dict diff --git a/course_utils/postprocessing.py b/course_utils/postprocessing.py index d2cd661..da0aba4 100644 --- a/course_utils/postprocessing.py +++ b/course_utils/postprocessing.py @@ -1,6 +1,5 @@ import json import re -from copy import deepcopy import click from pathlib import Path import logging diff --git a/requirements.txt b/requirements.txt index 1fe49c6..d6b5194 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,6 +8,7 @@ greenlet==2.0.2 guzzle-sphinx-theme==0.7.11 imagesize==1.4.1 importlib-metadata==6.7.0 +Jinja2==3.1.2 jupyter-book==0.15.1 jupyter-cache==0.6.1 jupyter-contrib-core==0.4.2 @@ -27,6 +28,7 @@ nbclient==0.7.4 pybtex==0.24.0 pybtex-docutils==1.0.2 pydata-sphinx-theme==0.13.3 +PyYAML==6.0 snowballstemmer==2.2.0 Sphinx==5.0.2 sphinx-book-theme==1.0.1 diff --git a/textbook/_templates/parsons-layout.html b/textbook/_templates/parsons-layout.html index 9806692..03efb33 100644 --- a/textbook/_templates/parsons-layout.html +++ b/textbook/_templates/parsons-layout.html @@ -20,7 +20,21 @@

  • Click the Reset button to clear the code space on the right and start over.
  • -
    +
    + {% if parsons.python_setup %} +

    Problem Setup

    +
    +

    Assume that the following code has run already in this session.

    + +
    
    +{% for line in parsons.python_setup %}
    +{{ line }}
    +{% endfor %}
    +
    +
    + {% endif %} +
    +

    Compose your code here

    diff --git a/textbook/parsons-assets/parsons-problems/css/parsons.css b/textbook/parsons-assets/parsons-problems/css/parsons.css index 7fd808f..28bdeea 100644 --- a/textbook/parsons-assets/parsons-problems/css/parsons.css +++ b/textbook/parsons-assets/parsons-problems/css/parsons.css @@ -1,10 +1,15 @@ .problem-statement { - font-size: 22; + font-size: 22px; } +.instructions, .parsons-setup { + font-size: 18px; +} + +.parsons-setup > pre { + margin-left: 20px; + margin-top: -30px; /* Gets rid of excess white space at top of code block */ -.instructions { - font-size: 18; } .parsons-container { display: grid; diff --git a/textbook/parsons-assets/parsons-problems/html/homework-1-1.html b/textbook/parsons-assets/parsons-problems/html/homework-1-1.html index f848838..8f64a86 100644 --- a/textbook/parsons-assets/parsons-problems/html/homework-1-1.html +++ b/textbook/parsons-assets/parsons-problems/html/homework-1-1.html @@ -20,7 +20,10 @@

  • Click the Reset button to clear the code space on the right and start over.
  • -
    +
    + +
    +

    Compose your code here

    @@ -33,25 +36,25 @@

    Compose your code here

    - price_num = price[1:] + book_prices = ['$55.99', '$119.95', '$13.95']
    - for price in book_prices: + price_num = float(price_num) * 1.1
    - book_prices = ['$55.99', '$119.95', '$13.95'] + price_num = price[1:]
    - price_num = float(price_num) * 1.1 + for price in book_prices:
    diff --git a/textbook/parsons-assets/parsons-problems/html/homework-1-2.html b/textbook/parsons-assets/parsons-problems/html/homework-1-2.html index 4f4ce44..61ec206 100644 --- a/textbook/parsons-assets/parsons-problems/html/homework-1-2.html +++ b/textbook/parsons-assets/parsons-problems/html/homework-1-2.html @@ -20,44 +20,53 @@

  • Click the Reset button to clear the code space on the right and start over.
  • -
    +
    + +

    Problem Setup

    +
    +

    Assume that the following code has run already in this session.

    +
    
    +
    +courses = ['CHEM 1001 10', 'CHEM 1001 11', 'BISC 1111 10', 'BISC 2207 10', 'PSC 1001 10',
    +
    +       'PSC 1001 11', 'PSC 1001 12', 'ANTH 3808 10', 'AMST 2071 80']
    +
    +
    +
    + +
    +

    Compose your code here

    - course_info = course.split() + print(depts)
    - depts.append(course_info[0]) + course_info = course.split()
    - depts = [] + depts.append(course_info[0])
    - for course in courses: + depts = []
    - courses = ['CHEM 1001 10', 'BISC 1111 10', 'BISC 2207 10'] -
    -
    - -
    -
    - print(depts) + for course in courses:
    @@ -110,15 +119,6 @@

    Compose your code here

    -
    - - -
    - -
    - -
    -

    diff --git a/textbook/parsons-assets/parsons-problems/html/homework-2-GR.html b/textbook/parsons-assets/parsons-problems/html/homework-2-GR.html index d8c21ef..dc53ab7 100644 --- a/textbook/parsons-assets/parsons-problems/html/homework-2-GR.html +++ b/textbook/parsons-assets/parsons-problems/html/homework-2-GR.html @@ -20,68 +20,71 @@

  • Click the Reset button to clear the code space on the right and start over.
  • -
    +
    + +
    +

    Compose your code here

    - courses_db.append(course_dict) + course_dict['section'] = course_info[2]
    - course_info = course.split() + courses_db.append(course_dict)
    - course_dict['dept'] = course_info[0] + courses = ['CHEM 1001 10', 'CHEM 1002, 10', 'CHEM 1002 11', 'BISC 1100 10']
    - print(courses_db) + courses_db = []
    - course_dict['course'] = course_info[1] + for course in courses:
    - course_dict = {} + course_dict['dept'] = course_info[0]
    - for course in courses: + course_dict['course'] = course_info[1]
    - courses_db = [] + print(courses_db)
    - course_dict['section'] = course_info[2] + course_dict = {}
    - courses = ['CHEM 1001 10', 'CHEM 1002, 10', 'CHEM 1002 11', 'BISC 1100 10'] + course_info = course.split()
    diff --git a/textbook/parsons-assets/parsons-problems/html/lessons-2-1-1.html b/textbook/parsons-assets/parsons-problems/html/lessons-2-1-1.html index a535edb..cf7de81 100644 --- a/textbook/parsons-assets/parsons-problems/html/lessons-2-1-1.html +++ b/textbook/parsons-assets/parsons-problems/html/lessons-2-1-1.html @@ -20,7 +20,10 @@

  • Click the Reset button to clear the code space on the right and start over.
  • -
    +
    + +
    +

    Compose your code here

    @@ -39,13 +42,13 @@

    Compose your code here

    - print(team) + for person in team:
    - for person in team: + print(team)
    diff --git a/textbook/parsons-assets/parsons-problems/js/parsons.js b/textbook/parsons-assets/parsons-problems/js/parsons.js index 07c574b..c7e6698 100644 --- a/textbook/parsons-assets/parsons-problems/js/parsons.js +++ b/textbook/parsons-assets/parsons-problems/js/parsons.js @@ -153,6 +153,16 @@ async function runPyodide(codeLines, pyodidePromise) { } } +async function runPyodideSetup(pyodidePromise) { + // Runs any setup code that should be executed in global scope, before the solution is executed + let setupCode = document.querySelector("#setup-code"); + + if (setupCode){ + let pyodide = await pyodidePromise; + await pyodide.runPython(setupCode.textContent); + } +} + function handlePyException(excProxy) { // Handles a dict returned from the Python reformat_exception function function extractLineNo(tbArray) { @@ -268,15 +278,17 @@ window.addEventListener("DOMContentLoaded", async () => { // button to trigger code execution const button = document.querySelector(".parsons-submit"); - button.addEventListener("click", (event) => { + button.addEventListener("click", async (event) => { // clear output area before running new code clearOutput(); let codeToRun = parseParsonsCode(); - runPyodide(codeToRun, pyodidePromise); + await runPyodide(codeToRun, pyodidePromise); }); document.querySelector(".parsons-reset").addEventListener("click", resetProblem); + await runPyodideSetup(pyodidePromise); + }); function getPreviousSiblings(elem, filter) { diff --git a/textbook/parsons-yaml/homework-1-2.yml b/textbook/parsons-yaml/homework-1-2.yml index e4578a9..eadb847 100644 --- a/textbook/parsons-yaml/homework-1-2.yml +++ b/textbook/parsons-yaml/homework-1-2.yml @@ -1,5 +1,7 @@ +python_setup: | + courses = ['CHEM 1001 10', 'CHEM 1001 11', 'BISC 1111 10', 'BISC 2207 10', 'PSC 1001 10', + 'PSC 1001 11', 'PSC 1001 12', 'ANTH 3808 10', 'AMST 2071 80'] python_code: | - courses = ['CHEM 1001 10', 'BISC 1111 10', 'BISC 2207 10'] depts = [] for course in courses: course_info = course.split()