Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

: Can't generate .pdf versions of quizzes: UnboundLocalError: local variable 'proc' referenced before assignment #47

Closed
mikegilchrist opened this issue Nov 5, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@mikegilchrist
Copy link

mikegilchrist commented Nov 5, 2021

I'm using text2qti 0.6.0.dev5 which I installed from github. I can create the QTI .zip files from my .md files, but when trying to create .pdf versions of my quizzes, I get the following error

$ text2qti --solutions test.pdf test.md
Traceback (most recent call last):
  File "/home/user/.local/lib/python3.7/site-packages/text2qti-0.6.0.dev5-py3.7.egg/text2qti/cmdline.py", line 144, in main
  File "/home/user/anaconda3/lib/python3.7/subprocess.py", line 512, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['pandoc', '-f', 'markdown', '-o', '/tmp/test.pdf']' returned non-zero exit status 43.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.local/bin/text2qti", line 11, in <module>
    load_entry_point('text2qti==0.6.0.dev5', 'console_scripts', 'text2qti')()
  File "/home/user/.local/lib/python3.7/site-packages/text2qti-0.6.0.dev5-py3.7.egg/text2qti/cmdline.py", line 147, in main
UnboundLocalError: local variable 'proc' referenced before assignment

I get the same behavior if I use the --only-solutions option.

I've looked at the code in text2qti/cmdline.py, but can't see why it's a problem. This seems to be the relevant code

                if solutions_path.suffix.lower() == '.pdf':
                    if not shutil.which('pandoc'):
                        raise Text2qtiError('Exporting solutions in PDF format requires Pandoc (https://pandoc.org/)')
                    if not shutil.which('pdflatex'):
                        raise Text2qtiError('Exporting solutions in PDF format requires LaTeX (https://www.tug.org/texlive/ or https://miktex.org/)')
                    if platform.system() == 'Windows':
                        cmd = [shutil.which('pandoc'), '-f', 'markdown', '-o', str(solutions_path)]
                    else:
                        cmd = ['pandoc', '-f', 'markdown', '-o', str(solutions_path)]
                    try:
                        proc = subprocess.run(
                            cmd,
                            input=solutions_text,
                            capture_output=True,
                            check=True,
                            encoding='utf8'
                        )
                    except subprocess.CalledProcessError:
                        raise Text2qtiError(f'Pandoc failed:\n{"-"*78}\n{proc.stderr}\n{"-"*78}')

Here's my test file

% For use with text2qti
Quiz title: Test file
Quiz description: Simple test file for text2qti


% Khan Academy

Title: Key Components of TF Regulation
Points: 8
1. Describe and differentiate between they key components of TF regulation systems: Activators, Repressors, Silencers, and Enhancers.
   Be sure to indicate which are made of DNA and which are made of protein.
... TF are made of protein while elements are made of DNA.
    Activators are TF that bind enhancer elements to promote the transcription of a gene.
    In contrast, repressors are TF that bind to silencer elements which prevent gene transcription.
___

I apologize if this is really a user error, but do appreciate the package. It's very helpful

@gpoore
Copy link
Owner

gpoore commented Nov 5, 2021

There's a bug in the error handling code that is invoked when Pandoc fails. Currently, the error message is using proc.stderr, but there are failure modes where proc never gets defined. I'll have to reorganize this a little.

In terms of why Pandoc is failing, my guess would be that there are missing LaTeX packages. text2qti needs everything that Pandoc usually requires, plus fontawesome. Until the bug is fixed, you might try creating solutions in md format, and then try to convert that to PDF to see if Pandoc will provide additional details.

@gpoore gpoore added the bug Something isn't working label Nov 5, 2021
@mikegilchrist
Copy link
Author

Thanks for the work around. It takes care of my issue.

@mikegilchrist
Copy link
Author

Update: Problem disappears if you use a more recent version of pandoc, such as 2.16.2

@gpoore gpoore closed this as completed in 810db5a Aug 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants