6

I want to generate student feedback reports using a loop like this:

for student in studentList
    # Report/feedback content goes here:

    # Here I want to use text with variables for example
    student * " received " * xPoints

    "Q1"
    "Good effort but missing units"

    "Q2"
    "More text ..."

    # end of the feedback
end

My goal is to generate 30+ PDF files for all students, with the score for each question, supplemented with some free text for each student. One way I thought of would be to write to multiple TeX files and compile them to PDFs at the end.

I'm not determined to output PDFs, if there is a better method of generating multiple, human-readable reports programmatically in Julia.

2
  • Try searching for such package in the Python ecosystem and call it via PyCall. You should be able to find something that does not require a full LaTeX stack. Oct 31, 2018 at 21:31
  • Have you checked out Weave.jl yet? Nov 1, 2018 at 4:43

1 Answer 1

5

As for now we can start with the basics and output a HTML file, which is faster. You can use a templating library, and in this case we can use mustache. The template is hardcoded, but it is simple to include it in an external file.

Remember to install the templating library Mustache:

import Pkg; Pkg.add("Mustache")

The basic idea is the following:

  • have a list of dictionaries with the data
  • have a template of the report, where parts to be substituted are in {{ ... }} guards
  • save the report of a single student in a file html through iteration.

You can add some code to directly send a mail to the student, without even saving the file, if your computer is configured to do so (as long as you do not include external CSS, the mail will be formatted accordingly to the HTML instructions).

using Mustache

students = [
  Dict( "name" => "John", "surname" => "Smith", "mark" => 30 ),
  Dict( "name" => "Elisa", "surname" => "White", "mark" => 100 )
]

tmpl = mt"""
<html>
<body>
Hello <b>{{name}}, {{surname}}</b>. Your mark is {{mark}}
</body>
</html>
"""

for student in students
  rendered = render(tmpl, student)
  filename = string("/tmp/", student["name"], "_", student["surname"], ".html")
  open(filename, "w") do file
    write(file, rendered)
  end
end

The result for a single student is something like:

<html>
<body>
Hello <b>Elisa, White</b>. Your mark is 100
</body>
</html>

If you prefer a PDF, i think the faster way is to have a piece of LaTeX as template (in place of a HTML template), to export the result of Mustache into a file and then to compile it from the script with a system call:

using Mustache

students = [
  Dict( "name" => "John", "surname" => "Smith", "mark" => 30 ),
  Dict( "name" => "Elisa", "surname" => "White", "mark" => 100 )
]

tmpl = mt"""
\documentclass{standalone}

\begin{document}

Hello \textbf{ {{name}}, {{surname}}}. Your mark is ${{mark}}$.

\end{document}
"""

for student in students
  rendered = render(tmpl, student)
  filename = string("/tmp/", student["name"], "_", student["surname"], ".tex")
  open(filename, "w") do file
    write(file, rendered)
  end
  run(`pdflatex $filename`)
end

which results in something like:

enter image description here

A reference to Mustache.jl, where you can find some instructions on how to iterate over different questions with a single line of template. This is an example in which the marks are an array of values (again for tex):

using Mustache

students = [
  Dict( "name" => "John", "surname" => "Smith", "marks" => [25, 32, 40, 38] ),
  Dict( "name" => "Elisa", "surname" => "White", "marks" => [40, 40, 36, 35] )
]

tmpl = """
\\documentclass{article}

\\begin{document}

Hello \\textbf{ {{name}}, {{surname}} }. Your marks are: 
\\begin{itemize}
  {{#marks}} 
    \\item Mark for question is {{.}} 
  {{/marks}}
\\end{itemize}
\\end{document}
"""

for student in students
  rendered = render(tmpl, student)
  filename = string("/tmp/", student["name"], "_", student["surname"], ".tex")
  open(filename, "w") do file
    write(file, rendered)
  end
  run(`pdflatex $filename`)
end

which results in:

enter image description here

4
  • I love @Matteo Ragni suggestion! What is the difference between tmpl = mt""" and tmpl = """ ? In other words, what does mt do before """? Nov 1, 2018 at 6:43
  • How can one use two arrays at the same time? Specifically, I would like to print: \\item Mark for question 1 is 40 out of 50. * Text feedback from (an array of) strings. \\item Mark for question 2 is 40 out of 46. * Text feedback from a string variable, and so on ... Nov 1, 2018 at 7:05
  • In other words, I am asking how to index #marks within Mustache? Nov 1, 2018 at 7:14
  • mt is a mustache template, a particular string where string interpolation cannot happen, while in normal strings (""" only) it can happen. I searched for a way to "index an array" with the julia implementation, but there is only a limited support for it (github.com/jverzani/Mustache.jl#iterating-over-vectors). There is simple solution though: since you can pass more than one variable to the render function, you can use a Data frame with two or more columns. Mustache will iterate through the rows. Take a look at the documentation, if I can find some time I will prepare an examplre Nov 1, 2018 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.