Skip to content

Instantly share code, notes, and snippets.

@jlumbroso
Created September 23, 2018 04:55
Show Gist options
  • Save jlumbroso/60971ec65324603c0ab2efd02fd8098b to your computer and use it in GitHub Desktop.
Save jlumbroso/60971ec65324603c0ab2efd02fd8098b to your computer and use it in GitHub Desktop.
Minimal example to build an Anki deck with an image, with genanki
import genanki
my_model = genanki.Model(
1380120064,
'Example',
fields=[
{'name': 'Object'},
{'name': 'Image'},
],
templates=[
{
'name': 'Card 1',
'qfmt': '{{Object}}',
'afmt': '{{FrontSide}}<hr id="answer"><img src="{{Image}}"/>',
},
])
my_note = genanki.Note(
model=my_model,
fields=['JPEG File', 'format.jpg'])
my_deck = genanki.Deck(
2059400191,
'Example')
my_deck.add_note(my_note)
my_package = genanki.Package(my_deck)
my_package.media_files = ['format.jpg']
my_package.write_to_file('output.apkg')
@GlosuU
Copy link

GlosuU commented Jun 6, 2023

Hi, I ran this script in my Macbook and when importing to Anki, the image is broken/doesn't appear. I of course provided a format.jpg image in the root directory of the script, but it's not showing this image. Any clues as to what I could be missing? Do I need to tweak any settings in Anki itself?

@jlumbroso
Copy link
Author

Hello @GlosuU: Could you take a look at this other Gist and let me know if it works better for you?
https://gist.github.com/jlumbroso/e17abff02d89be04240072191af09ab2

I think I have two versions of this example, and one doesn't work, and I think it's this one—just going by the number of stars. 😁

@GlosuU
Copy link

GlosuU commented Jun 6, 2023

@jlumbroso Thanks for the prompt reply and yes, you are indeed right, the other script worked perfectly for me! Maybe you should consider deleting this file since it was the file the Google Search Engine directed me to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment