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

Allow image size to be changed #41

Open
gordonwatts opened this issue Mar 15, 2021 · 8 comments
Open

Allow image size to be changed #41

gordonwatts opened this issue Mar 15, 2021 · 8 comments

Comments

@gordonwatts
Copy link

There are times that images on disk aren't the proper size (too big?) to be displayed in the exam. The Canvas quiz-question editor allows one to change the image size by hand. Short of using a full <img ... .> tag in your markdown there isn't a way to do that inline in hte markdown.

@gordonwatts
Copy link
Author

One possible solution is to use the markdown extension called attr_list, which allows you to set attributes for a html element created in markdown. This is part of the markdown processor that text2qti is already using; it is just a matter of turning it on.

This commit to my fork of text2qti does that.

If you are interested in this as a solution to this problem (or any other generic one that attributes might solve), I'm happy to submit a PR.

@gpoore
Copy link
Owner

gpoore commented Mar 15, 2021

Support for attributes in general is a good suggestion. I think I looked at the attr_list extension once, but didn't turn it on because I'm trying to keep everything as compatible as possible with Pandoc Markdown. I just took a quick look at the attr_list extension source (https://github.com/Python-Markdown/markdown/blob/master/markdown/extensions/attr_list.py). It would probably be easy to create a custom version that is more or less compatible with Pandoc. Basically, swap opening delimiter from {: to { and then limit what can have attributes. I suppose it would also be possible to add an option to enable the existing attr_list as-is.

@gordonwatts
Copy link
Author

Ok - finished finals for this quarter.

If it is possible to change, then I'd suggest not using the { since other things are changing because that is a special character to python (in f-strings).

I'm ok with creating a new flag and turning on and off attr_list. If you are interested in that PR let me know. If you want to modify attr_list' source, that might require a little more work, and perhaps should be done with pandoc in mind (and I've never used pandoc).

@gpoore
Copy link
Owner

gpoore commented Apr 19, 2021

I've added support in the last commit for Pandoc-style attributes on images. This is similar to attr_list, but uses {<attrs>} instead of {:<attrs>}. Currently, attributes are only supported on images, but that can be easily extended in the future. Since the syntax is compatible with Pandoc, this feature automatically works with solutions export.

@gordonwatts
Copy link
Author

I think this takes care of this - thanks! Feel free to close this!

@tdstoff
Copy link

tdstoff commented Sep 4, 2024

Can anyone provide an example of this working with an actual image file? Specifically changing the image size. I haven't found it in the README or anywhere else.

For example, I try below to do a simple modification to an example file in a repo here. In the example, the image files are generated by matplotlib on the fly. My only addition was trying to modify the width of one of the images with {width="100px"}.

import textwrap
import matplotlib.pyplot as plt
for c in ['red', 'blue']:
    plt.plot([1,2,3,4], color=c)
    plt.ylabel('some numbers')
    plt.savefig(c+'.png')
q=rf"""
1. What is your favorite color?
[*] ![The data for the question](red.png){width="100px"}
[*] ![The data for the question](blue.png)
"""
print(textwrap.dedent(q))

Any time I add the attributes, I get various errors depending on the question type and whether the image is part of the question or answer. Here it gives:

Code execution resulted in errors:
--------------------------------------------------
  File "/var/folders/d9/n16xn0557wqbvqhcywpth__m0000gn/T/tmpm5c5ipwz/be906e50bc50f19c.code", line 9
    [*] ![The data for the question](red.png){width="100px"}
                                                    ^^^^^^^
SyntaxError: f-string: expecting '!', or ':', or '}'

--------------------------------------------------

@gpoore
Copy link
Owner

gpoore commented Sep 4, 2024

@tdstoff The admittedly very short relevant documentation is here: https://github.com/gpoore/text2qti?tab=readme-ov-file#images. The error that you provided is due to the format string. You can either switch to a regular string (remove f) or double the braces {{...}}. The width shouldn't be quoted ({width=100px}); I should add a note to the documentation that quoting isn't supported, or see if I can add quote support.

@tdstoff
Copy link

tdstoff commented Sep 4, 2024

That works, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants