PyQuiz 1.0 – Create tests with Python… in html

I made an update of this program to make simple test in html, but through Python that takes the hard work of writing the html code for you. I splitted the code into two files that have to go in the same directory. One contains just the data (the introductory text and the list with questions and answer). The other file has all the code to create the html file. You need to launch the first file only. You will have the html opened and working from your browser without having nothing to do apart get the html code and copy it in your blog or use it locally.

Here is the code with some questions:

from questions_engine import *


# This is the introductory test
# You can use html
# * will be replaced by <h2>...</h2>)
traccia1 = """*Budget economico
Il budget economico prevede i costi e i ricavi dell'esercizio successivo. 
Deriva dai budget settoriali che, a loro volta [...]
"""


# List of lists with question and right answer
# what if I want to use a multiple choice?
sol = [
    ["Produzione del 2010", "1.350.000"],
    ["Inserisci i ricavi del 2010", "1.350.000"],
    ["Inserisci i costi del 2010", "1.000.000"],
    ["Calcola l'utile del 2010", "350.000"],
    ["<h1>2013</h1>"],
    ["Produzione del 2013", "2.430.000"],
    ["Calcola i ricavi del 2013", "2.352.240"],
    ["Calcola i costi del 2013", "1.700.000"],
    ["A quanto ammonta l'utile del 2013?", "652.240"],
]


html = create_html(traccia1, sol)
save(html)

This is where your data are. You can make one of this for each test and everyone will use the following file to “render” the file. Call the file questions_engine.py. You have to launch only the file above, but you need this in the same directory.

# A script to make tests in javascript with python
import os
import re

# List of lists with datas for the questions; each has 1.image 2.question 3.answer all as strings
# Adds the initial script and the html code with the questions
style = """<style> body {font-size : 2em; } input {font-size: 3em;width:100%; background: cyan; } button { font-size: 3em; } </style> """


# Here goes the function that checks if the answer is right
function = """<script>
function code(val, sol){ if (val==sol) return 1;}
</script>
"""
# This is the input text template that will be used to create the questions

but = """<button id="---idbut---1" value="1" onclick="x=code(this.value, '--soluzione--');console.log(this.value);if (x==1){this.style.background='yellow';--xxx--.value='ok'}; if (x!=1){ --xxx--.value='no'};---idbut---1.disabled=true;---idbut---2.disabled=true;---idbut---3.disabled=true;--xxx--.disabled=true">1</button>---ans1---<br>"""
but += """<button id="---idbut---2" value="2" onclick="x=code(this.value, '--soluzione--');console.log(this.value);if (x==2){this.style.background='yellow';--xxx--.value='ok'}; if (x!=2){ --xxx--.value='no';---idbut---1.disabled=true;---idbut---2.disabled=true;---idbut---3.disabled=true;--xxx--.disabled=true}"">2</button>---ans2---<br>"""
but += """<button id="---idbut---3" value="3" onclick="x=code(this.value, '--soluzione--');console.log(this.value);if (x==3){this.style.background='yellow';--xxx--.value='ok'}; if (x!=3){ --xxx--.value='no';---idbut---1.disabled=true;---idbut---2.disabled=true;---idbut---3.disabled=true;--xxx--.disabled=true}"">3</button>---ans3---<br>"""

tpl = """<br>--img--<b>--question--</b>
---but---"""

text = """
<input id="--xxx--" type="text" onchange="x=code(this.value, '--soluzione--');if (x==1){this.style.background='yellow'};--xxx--.disabled=true">
"""

def add_tag_to(text):
    # * is for header 2
    #search all the words with a *
    # You need a \*, because * has a meaning in regex
    pattern = "\*.+"
    h1 = re.findall(pattern, text)
    for word in h1:
        word = word.replace("*", "")
        tagged = re.sub(pattern, f"<h2>{word}</h2>", text)
        # delete that \n from <h2> line, to avoid double <br>
        tagged = tagged.replace("\n", "")
    # ============================ header 2
    # This will add a break to every \n, to avoid having to add it manually
    tagged = re.sub("\n", "<br>", tagged)
    return tagged

def create_html(traccia1, sol):
    "This returns the html code with the questions and input boxes"
    imgurl = "http://icons.iconarchive.com/icons/tatice/cristal-intense/256/Question-icon.png"

    # This adds some shortcut to html tags in the text (see the function)
    traccia1 = add_tag_to(traccia1)
    imgtag = "<img src='" + imgurl + "'' width=50/>"
    html = style + traccia1 + function
    counter = 0
    # substitute the placeholders with data
    for qns in sol:
        template = tpl
        if len(qns) == 1:
            template = qns[0]
        else:
            # if you add a paragraph as second item it package it as a regular 2 item list
            if "-but-" in qns[0]:
                template = template.replace("---but---", but)
                qns[0] = qns[0].replace("-but-", "...")
                template = template.replace("---idbut---", "idbut" + str(counter))
                #qns[1] = "<ol>" + "<li>".join(qns[1].split("#")) + "</ol>"

            else:
                template = template.replace("---but---", "")
                template = template + text

            template = template.replace("--img--", imgtag)
            template = template.replace("--question--", qns[0])
            #
            if "..." not in qns[0]:
                template = template.replace("--soluzione--", qns[1])
            else:
                template = template.replace("...", "")
            template = template.replace("--xxx--", "id" + str(counter))
            template += "<hr>"
            counter += 1
        html += template
    return html

def save(html):
    # Saves the html file with the code created above
    with open("esempio.html", "w") as file:
        file.write(html)
    # shows the file in the browser
    os.startfile("esempio.html")

You will notice that there is some code that is not used in this file, because I wanted to add also a way to make multiple choices questions, but this will be done in the next update.

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.