{"id":4760,"date":"2020-01-09T19:04:01","date_gmt":"2020-01-09T18:04:01","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=4760"},"modified":"2020-01-09T19:04:01","modified_gmt":"2020-01-09T18:04:01","slug":"pyquiz-1-0-create-tests-with-python-in-html","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/pyquiz-1-0-create-tests-with-python-in-html\/","title":{"rendered":"PyQuiz 1.0 &#8211; Create tests with Python&#8230; in html"},"content":{"rendered":"<p>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.<\/p>\n<p>Here is the code with some questions:<\/p>\n<pre class=\"lang:default decode:true\">from questions_engine import *\r\n\r\n\r\n# This is the introductory test\r\n# You can use html\r\n# * will be replaced by &lt;h2&gt;...&lt;\/h2&gt;)\r\ntraccia1 = \"\"\"*Budget economico\r\nIl budget economico prevede i costi e i ricavi dell'esercizio successivo. \r\nDeriva dai budget settoriali che, a loro volta [...]\r\n\"\"\"\r\n\r\n\r\n# List of lists with question and right answer\r\n# what if I want to use a multiple choice?\r\nsol = [\r\n    [\"Produzione del 2010\", \"1.350.000\"],\r\n    [\"Inserisci i ricavi del 2010\", \"1.350.000\"],\r\n    [\"Inserisci i costi del 2010\", \"1.000.000\"],\r\n    [\"Calcola l'utile del 2010\", \"350.000\"],\r\n    [\"&lt;h1&gt;2013&lt;\/h1&gt;\"],\r\n    [\"Produzione del 2013\", \"2.430.000\"],\r\n    [\"Calcola i ricavi del 2013\", \"2.352.240\"],\r\n    [\"Calcola i costi del 2013\", \"1.700.000\"],\r\n    [\"A quanto ammonta l'utile del 2013?\", \"652.240\"],\r\n]\r\n\r\n\r\nhtml = create_html(traccia1, sol)\r\nsave(html)\r\n<\/pre>\n<p>This is where your data are. You can make one of this for each test and everyone will use the following file to &#8220;render&#8221; the file. Call the file questions_engine.py. You have to launch only the file above, but you need this in the same directory.<\/p>\n<pre class=\"lang:default decode:true \"># A script to make tests in javascript with python\r\nimport os\r\nimport re\r\n\r\n# List of lists with datas for the questions; each has 1.image 2.question 3.answer all as strings\r\n# Adds the initial script and the html code with the questions\r\nstyle = \"\"\"&lt;style&gt; body {font-size : 2em; } input {font-size: 3em;width:100%; background: cyan; } button { font-size: 3em; } &lt;\/style&gt; \"\"\"\r\n\r\n\r\n# Here goes the function that checks if the answer is right\r\nfunction = \"\"\"&lt;script&gt;\r\nfunction code(val, sol){ if (val==sol) return 1;}\r\n&lt;\/script&gt;\r\n\"\"\"\r\n# This is the input text template that will be used to create the questions\r\n\r\nbut = \"\"\"&lt;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\"&gt;1&lt;\/button&gt;---ans1---&lt;br&gt;\"\"\"\r\nbut += \"\"\"&lt;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}\"\"&gt;2&lt;\/button&gt;---ans2---&lt;br&gt;\"\"\"\r\nbut += \"\"\"&lt;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}\"\"&gt;3&lt;\/button&gt;---ans3---&lt;br&gt;\"\"\"\r\n\r\ntpl = \"\"\"&lt;br&gt;--img--&lt;b&gt;--question--&lt;\/b&gt;\r\n---but---\"\"\"\r\n\r\ntext = \"\"\"\r\n&lt;input id=\"--xxx--\" type=\"text\" onchange=\"x=code(this.value, '--soluzione--');if (x==1){this.style.background='yellow'};--xxx--.disabled=true\"&gt;\r\n\"\"\"\r\n\r\ndef add_tag_to(text):\r\n    # * is for header 2\r\n    #search all the words with a *\r\n    # You need a \\*, because * has a meaning in regex\r\n    pattern = \"\\*.+\"\r\n    h1 = re.findall(pattern, text)\r\n    for word in h1:\r\n        word = word.replace(\"*\", \"\")\r\n        tagged = re.sub(pattern, f\"&lt;h2&gt;{word}&lt;\/h2&gt;\", text)\r\n        # delete that \\n from &lt;h2&gt; line, to avoid double &lt;br&gt;\r\n        tagged = tagged.replace(\"\\n\", \"\")\r\n    # ============================ header 2\r\n    # This will add a break to every \\n, to avoid having to add it manually\r\n    tagged = re.sub(\"\\n\", \"&lt;br&gt;\", tagged)\r\n    return tagged\r\n\r\ndef create_html(traccia1, sol):\r\n    \"This returns the html code with the questions and input boxes\"\r\n    imgurl = \"http:\/\/icons.iconarchive.com\/icons\/tatice\/cristal-intense\/256\/Question-icon.png\"\r\n\r\n    # This adds some shortcut to html tags in the text (see the function)\r\n    traccia1 = add_tag_to(traccia1)\r\n    imgtag = \"&lt;img src='\" + imgurl + \"'' width=50\/&gt;\"\r\n    html = style + traccia1 + function\r\n    counter = 0\r\n    # substitute the placeholders with data\r\n    for qns in sol:\r\n        template = tpl\r\n        if len(qns) == 1:\r\n            template = qns[0]\r\n        else:\r\n            # if you add a paragraph as second item it package it as a regular 2 item list\r\n            if \"-but-\" in qns[0]:\r\n                template = template.replace(\"---but---\", but)\r\n                qns[0] = qns[0].replace(\"-but-\", \"...\")\r\n                template = template.replace(\"---idbut---\", \"idbut\" + str(counter))\r\n                #qns[1] = \"&lt;ol&gt;\" + \"&lt;li&gt;\".join(qns[1].split(\"#\")) + \"&lt;\/ol&gt;\"\r\n\r\n            else:\r\n                template = template.replace(\"---but---\", \"\")\r\n                template = template + text\r\n\r\n            template = template.replace(\"--img--\", imgtag)\r\n            template = template.replace(\"--question--\", qns[0])\r\n            #\r\n            if \"...\" not in qns[0]:\r\n                template = template.replace(\"--soluzione--\", qns[1])\r\n            else:\r\n                template = template.replace(\"...\", \"\")\r\n            template = template.replace(\"--xxx--\", \"id\" + str(counter))\r\n            template += \"&lt;hr&gt;\"\r\n            counter += 1\r\n        html += template\r\n    return html\r\n\r\ndef save(html):\r\n    # Saves the html file with the code created above\r\n    with open(\"esempio.html\", \"w\") as file:\r\n        file.write(html)\r\n    # shows the file in the browser\r\n    os.startfile(\"esempio.html\")\r\n<\/pre>\n<p>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.<\/p>\n<p><iframe loading=\"lazy\" title=\"A quiz in html made with Python\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/SUxTR3k6AMY?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n\t<script>\r\nvar title = \"Exercises\";\r\n\t\tvar links = [\r\n[\"https:\/\/pythonprogramming.altervista.org\/pyquiz-1-0-create-tests-with-python-in-html\/\",\"Python to make test in Html\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-writes-short-answers-exercises-in-javascript\/\",\"Python and Javascript short answer exercises\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-to-make-exercises-in-javascript-yes-why-not\/\",\"Python and Javascript to make Interactive tests\"],\r\n[\"https:\/\/www.youtube.com\/watch?v=Y2XIEFqBCDc\",\"Video Exercises part 1\"],\r\n[\"https:\/\/www.youtube.com\/watch?v=FcyhUPRTv6c&t=210s\",\"Video Exercises part 2\"]\t\t\t\r\n];\r\n\t\t\r\n<\/script>\r\n<script>\r\n\t\r\nif (typeof next2 != \"undefined\"){let next2 = 0;}\r\n\t\r\nnext2 = 0;\r\n\thtml = \"\";\/\/<b style='color:coral;font-size:1.2em'>Other posts about \" + title + \"<\/b><br>\";\r\nfor (address of links) \r\n{\r\n\r\n\tif (next2 == 1){\r\n\t\thtml += \"<div style='background:coral'>\";\r\n\t\thtml += \"Next link => <a href='\" + address[0] + \"'>\" + address[1] + \"<\/a>\";\r\n\t\thtml += \"<\/div><br>\";\r\n\t\tnext2 = 0;\r\n\t}\r\n\tif (address[0] == document.URL) {\r\n\t\tnext2 = 1;\r\n\t}\r\n}\r\n\r\nif (typeof next != \"undefined\") {let next = 0;}\r\nif (typeof addressStart != \"undefined\") {let addressStart = \"\";}\r\nnext = 0;\r\naddressStart = \"<a href='\";\r\nfor (address of links) {\r\n\tif (next == 1){\r\n\t\thtml += \">>>\" + addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t\tnext = 0;\r\n\t}\r\n\telse if (addressStart + address[0] != document.URL)\r\n\t{\r\n\t\thtml += addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t}\r\n\telse\r\n\t{\r\n\t\tnext = 1;\r\n\t\tnext_address = address[0]\r\n\t\tnext_title = address[1]\r\n\t\thtml += \"<span style='color:gray'>\" + address[1] + \"<\/span><br>\";\r\n\t}\r\n\r\n}\r\n\r\n\thtml += `<span style=\"font-size:8px\">Powered by <a href=\"https:\/\/pythonprogramming.altervista.org\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2673\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png\" alt=\"\" width=\"70\" height=\"25\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png 156w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2-150x56.png 150w\" sizes=\"auto, (max-width: 70px) 100vw, 70px\" \/>pythonprogramming.altervista.org<\/a><\/span>`\r\n\thtml = \"<div style='background:yellow'>\" + html + \"<\/div>\";\r\n\tdocument.write(html)\r\n<\/script>\r\n\t\t\n","protected":false},"excerpt":{"rendered":"A quiz in html, made with Python\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/pyquiz-1-0-create-tests-with-python-in-html\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":4761,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[666],"tags":[468,627,103,126],"class_list":["post-4760","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-education","tag-education","tag-exercises","tag-quiz","tag-test"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":true,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/4760","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=4760"}],"version-history":[{"count":1,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/4760\/revisions"}],"predecessor-version":[{"id":4762,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/4760\/revisions\/4762"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/4761"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=4760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=4760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=4760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}