Asset Manager [Official support]

Hi,
I’ve figured out how to use the addon but, as I have explained in my other post:

I have a note type with around 40 cards, each of which have a slightly different template. For example, the first card might reference fields 01, 02 & 03, and the second card reference fields 02, 03 & 04, and so on.

I want to add fields that end in current card’s number minus 1 or plus 1 or 2, etc. They are in this format Field01
Is there a way to do it automatically?

Hi, I’m having trouble with getting a script to function when it’s inserted into the template via asset manager.

The Script
function doc_keyUp(e) {
    if (e.key === 'h') {
        flipVisibility('hintzu');
    }
}
document.addEventListener('keyup', doc_keyUp, false);

I can use it no problem when I include it inside normal <script></script> anywhere in the template. But when it’s inserted via asset manager, it does not function.

The function that is called on keypress:
 function flipVisibility(elementId) {
            if (document.getElementById(elementId).style.display=='none') {
                document.getElementById('hintzu') .style.display='';
            }
            else{
                document.getElementById(elementId).style.display='none';
            }
        }

And this function is also called in the onclick of a html button in the card:

<button class="hintzubutton" onclick=" flipVisibility('hintzu')" type="button">

I have tried:

  1. Inserting them both inside one script in asset manager - the button works but the keypress doesn’t
  2. Inserting them using separate scripts in asset manager - same as above.
  3. Inserting just the keypress script directly into the template using <script> and the function via asset manager - both work.

Anki Version: 2.1.41, OS: Windows, addon fully updated.

After inserting it via Asset Manager, you can still check out the template and see what Asset Manager inserted exactly. Maybe that will clear up things.

Well yes I did check the templates, and it had inserted the correct scripts.
Please disregard the previous post, because it turns out the problem is probably not with asset manager but with the script or anki itself.
I tried inserting it directly into the templates and it seems to work only sometimes. And upon further trials, it sometimes works when inserted via asset manager too.
It works every alternate time that the particular side of the card is rendered. Tested by repeatedly pressing the ‘Back Template’ Radio button in the ‘Card Types Window’, which renders that side of the card anew. Can’t figure the reason for this odd behaviour.


Also, I couldn't find it in the addon's page, but can we use asset manager to manage the styling section too?

@Bakr_10 Sorry, this was not meant as an answer to you, happened on accident.

Provided this gets merged, the insertion of CSS wrapped in <style> tags will probably be possible with the next update.

This will however still insert the content into the HTML templates (Front | Back).

Perhaps a separate tab for the stylesheet, where the content gets inserted into the Styling section of the template, would be a good addition?

1 Like

Could you send me some sample cards of your note type?
I think it could be solved with some creative renaming of the fields.


@kleinerpirat good to know, thanks

Sorry for cutting in.

Maybe the cause is that the following line registers an event listener cumulatively on the document object each time a card is displayed.

Anki Desktop only updates part of the document each time a card is displayed. Therefore, once an event listener is registered on the document, it will remain there unless you execute removeEventListener().

screenshot

There are several ways to deal with this, but in your case, I think it would be easy to check if the function exists in the global scope, and only if it does not, define the function in the global scope and execute addEventListener() as follows:

code
if (typeof doc_keyUp !== "function") {
    var doc_keyUp = function (e) {
        if (e.key === "h") {
            flipVisibility("hintzu");
        }
    };
    document.addEventListener("keyup", doc_keyUp, false);
}
3 Likes

Hi hengiesel,
Since the new Anki version is available since a couple of weeks, I hope Asset Manager will be compatible soon with the new version. I heard it doesnt work yet and i am just a little anxious that i will lose all my assets which took me a long time to create when i update anki. Anyway thanks a lot this addon makes using anki a lot easier for me.

I’ve already updated it last week for 2.1.47. So as long as your Anki is up-to-date, you should be fine :slight_smile:

1 Like

Great, thanks for the quick reply

1 Like

Asset Manager seems to be more unstable on 2.1.47 than it has been on 2.1.44. Edit: Unstable is the wrong word. It’s just less forgiving now, probably because Anki itself got a bit more strict.

I’ve encountered this error with two completely different note types on “Write to Templates”:

  File "/home/user/.local/share/Anki2/addons21/656021484/gui_config/config.py", line 58, in writeBackCurrentSetting
    self.write_back_callback(*self.export_data())
  File "/home/user/.local/share/Anki2/addons21/656021484/src/models.py", line 22, in write_back
    setup_model(model_id, html_data, script_data)
  File "/home/user/.local/share/Anki2/addons21/656021484/src/model_editor/__init__.py", line 9, in setup_model
    setup_full(model_id, html, scripts)
  File "/home/user/.local/share/Anki2/addons21/656021484/src/model_editor/setup_html.py", line 208, in setup_full
    insert(unminifieds, template_fmts, lambda: mw.col.models.update_dict(model))
  File "/home/user/.local/share/Anki2/addons21/656021484/src/model_editor/minify.py", line 22, in insert_unminified
    process_minifieds(unminifieds, template_fmts, callback)
  File "/home/user/.local/share/Anki2/addons21/656021484/src/model_editor/minifier.py", line 38, in process_minifieds
    callback()
  File "/home/user/.local/share/Anki2/addons21/656021484/src/model_editor/setup_html.py", line 208, in <lambda>
    insert(unminifieds, template_fmts, lambda: mw.col.models.update_dict(model))
  File "anki/models.py", line 223, in update_dict
  File "anki/_backend/generated.py", line 444, in update_notetype_legacy
  File "anki/_backend/__init__.py", line 131, in _run_command
anki.errors.TemplateError: Card template ⁨1⁩ in notetype '⁨SomeNoteType⁩' has a problem.<br>See the preview for more information.

It happened instantly on my main note type, on the other one it happened after adding new card templates. I feel like it might be caused by the now more aggressive template warning system on Anki’s side.

Could you make out what the exact error message is? Maybe I could add a workaround for it.

Sadly I couldn’t, because all of this happens in the background. There’s no way to “see the preview” when using Asset Manager.

I’ll try to find a way to reproduce this issue reliably…

This may be me being dumb, but I can’t work out how to open the main window for this add on…
I’ve tried:

  • Main Anki Tools menu (where add-ons normally are)
  • All the menus within edit card

The manager is accessible via an extra button in the “Manage Notetypes” screen, but in the add-on config, you can set it to also show a quick access button (Assets) in the editor.

1 Like

Thanks!

Is there an easy way to transfer scripts to other note types, without having to manually insert the same code into every note type?

Currently no, sorry.

Hello there,

I have been getting this error, and I suspect I have done something wrong, but I don’t know what:

Anki 2.1.54 (b6a7760c) Python 3.9.7 Qt 5.15.2 PyQt 5.15.5
Platform: Windows 10
Flags: frz=True ao=True sv=2
Add-ons, last update check: 2022-11-09 21:35:11

Caught exception:
Traceback (most recent call last):
File “C:\Users\uesr\AppData\Roaming\Anki2\addons21\656021484\gui_config\config.py”, line 58, in writeBackCurrentSetting
self.write_back_callback(*self.export_data())
File “C:\Users\user\AppData\Roaming\Anki2\addons21\656021484\src\models.py”, line 22, in write_back
setup_model(model_id, html_data, script_data)
File “C:\Users\user\AppData\Roaming\Anki2\addons21\656021484\src\model_editor_init_.py”, line 9, in setup_model
setup_full(model_id, html, scripts)
File “C:\Users\user\AppData\Roaming\Anki2\addons21\656021484\src\model_editor\setup_html.py”, line 200, in setup_full
if unminified := evaluate_fragment(
File “C:\Users\User\AppData\Roaming\Anki2\addons21\656021484\src\model_editor\setup_html.py”, line 143, in evaluate_fragment
text = base.code
AttributeError: ‘NoneType’ object has no attribute ‘code’

Any help would be much appreciated.

Anki 23.10.1 (fac9e0ee) Python 3.9.15 Qt 6.6.0 PyQt 6.6.0
Platform: Windows-10-10.0.19045
Flags: frz=True ao=True sv=3
Add-ons, last update check: 2023-12-06 10:43:06

When loading Asset Manager:
Traceback (most recent call last):
File “aqt.addons”, line 245, in loadAddons
File “D:\Users\X\AppData\Roaming\Anki2\addons21\656021484_init_.py”, line 10, in
from .src import setup
File “D:\Users\X\AppData\Roaming\Anki2\addons21\656021484\src_init_.py”, line 2, in
from .models import init_models_dialog
File “D:\Users\X\AppData\Roaming\Anki2\addons21\656021484\src\models.py”, line 7, in
from …gui_config.config import ConfigDialog
File “D:\Users\X\AppData\Roaming\Anki2\addons21\656021484\gui_config\config.py”, line 20, in
from .forms.config_ui import Ui_Config
File “D:\Users\X\AppData\Roaming\Anki2\addons21\656021484\gui_config\forms\config_ui.py”, line 11, in
from PyQt5 import QtCore, QtGui, QtWidgets
ModuleNotFoundError: No module named ‘PyQt5’