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

AnkiConnect doesn't export cards #44

Closed
devdouglasonofre opened this issue Feb 9, 2022 · 16 comments
Closed

AnkiConnect doesn't export cards #44

devdouglasonofre opened this issue Feb 9, 2022 · 16 comments

Comments

@devdouglasonofre
Copy link

devdouglasonofre commented Feb 9, 2022

Expected Behavior

The created cards should be automatically imported to Anki.

Current Behavior

Nothing happens. Not even an error is threw.

Possible Solution

Without any errors, it may be hard to debug. Although I think it's related to the AnkiConnect plugin.

Steps to Reproduce

  1. Create a phrase;
  2. Go to "Anki Export" route;
  3. Select a word/expression;
  4. Click on the "Add Card" button;
  5. Choose "In Context style";
  6. On the right side nav bar, click on "Export (1 card)" button;
  7. Select deck;
  8. Click on the "Add to Deck" button
  9. Screen says: "Congrats, you're done!", but nothing happened. No card is added to said deck (or any). Not even a XHR call.

Context (Environment)

Chrome 98.0.4758.81 64 bits.
Anki ⁨2.1.49 (dc80804a)⁩
AnkiConnect Lastest Version as from 2021-12-27.

@devdouglasonofre devdouglasonofre changed the title AnkiConnect doesn't import cards AnkiConnect doesn't export cards Feb 9, 2022
@sheodox
Copy link
Owner

sheodox commented Feb 9, 2022

Are you sure there isn't an XHR call at all? I'm looking at the code and I can see I don't have good enough error handling here yet so it's understandable why it's saying things "Congrats, you're done!", but it looks like it should be doing some XHR call to http://localhost:8765 at least. Can you double check?

Thank you for reporting this issue by the way!

Edit: Also, have you tried selecting a different or new deck?

@sheodox
Copy link
Owner

sheodox commented Feb 9, 2022

I deployed additional error handling. Now if an error occurs it should show the error in a toast and it won't say "Congrats, you're done!".

I also realized I didn't have anything preventing someone from trying to import cards when a deck hadn't been selected. That would cause things to break with a javascript error before doing any XHR like you're seeing.

Let me know if this fixes your issue!

@devdouglasonofre
Copy link
Author

devdouglasonofre commented Feb 10, 2022

Are you sure there isn't an XHR call at all?

Sorry. I expressed myself badly. There was XHR calls, both to check the Decks name and IDs, and to add the new cards, with all the data required, but the POST headers are always 200. And yes, the url is the same, and I created a new deck, tried to insert in a nested deck... No luck.


Tried again. Now with the Yomichan's Anki Integration turned off and... No luck. No card is getting added. Even the sync button on Anki that becomes blue as soon as the Anki detects a new change isn't turning blue. It's like the card not even reach to Anki.

And, as I said, there is no error, neither from the CR nor from Anki.

@sheodox
Copy link
Owner

sheodox commented Feb 10, 2022

and to add the new cards, with all the data required

What was in the response to the xhr to add the cards?

but the POST headers are always 200

As far as I remember Anki-Connect will always respond with a 200. The response changes if there's an error, but the http status will always be 200. If there's an error the response will be something like {"result": null, "error": "some error message"}, Is the response something like that?

@devdouglasonofre
Copy link
Author

devdouglasonofre commented Feb 10, 2022

What was in the response to the xhr to add the cards?

{"result": [null], "error": null}.

@sheodox
Copy link
Owner

sheodox commented Feb 11, 2022

Hmm, that is weird. It should normally be an array of numbers, not a null.
image

Out of curiosity do you happen to have the Browse window up? I'm seeing some similar sounding issues that mention that having the Browse window open would clear changes.
FooSoft/anki-connect#82

Could you post the Japanese phrase you're using, what word you're highlighting, and the name of the Anki deck you're trying to add it to? I wonder if there are some specific issues with deck names that I'm not aware of.

@devdouglasonofre
Copy link
Author

devdouglasonofre commented Feb 11, 2022

Out of curiosity do you happen to have the Browse window up

No. I just open the window to see if the cards are added, a few seconds after adding them through the CR.


Could you post the Japanese phrase you're using

勝者という概念がある以上敗者は同じくして存在する。

what word you're highlighting

存在 (both "Dictionary" and "In Context" formats. Edit: Not at the same time. I try with one then proceed to refresh and try with the other.)

and the name of the Anki deck you're trying to add it to

Tango N5+N4::CR Mining (tested with a new deck called "test" and Default, neither works.)

@sheodox
Copy link
Owner

sheodox commented Feb 12, 2022

Hmm, I'm kind of unsure what else to check, I tried all of those and it all worked for me.

If you go to the plugins menu in Anki, then click Anki-Conect and click "Config" can you comment with the JSON that shows up?

For reference mine looks like this:

{
    "apiKey": null,
    "apiLogPath": null,
    "ignoreOriginList": [],
    "webBindAddress": "127.0.0.1",
    "webBindPort": 8765,
    "webCorsOriginList": [
        "http://localhost",
        "https://context.reviews"
    ]
}

Also, what OS are you using? I've tried this on Linux (Fedora) and Windows and both work for me.

@devdouglasonofre
Copy link
Author

devdouglasonofre commented Feb 12, 2022

can you comment with the JSON that shows up?

{
    "apiKey": null,
    "apiLogPath": null,
    "webBindAddress": "127.0.0.1",
    "webBindPort": 8765,
    "webCorsOriginList": ["http://localhost", "https://context.reviews"],
    "ignoreOriginList": []
}

what OS are you using

Windows 10 Home Single Language 21H2.


Two details:
1- I'm using the Google Auth feature.
2- When I tried do add my first card, I created a deck through the CR. I don't know if it affects the Anki Connect in some way.

@sheodox
Copy link
Owner

sheodox commented Feb 12, 2022

Hmm, that all looks fine.

Can you try launching Anki from the command line and see if there are any Anki-Connect logs when you hit "Add to Deck" that show up and look alarming? I'm seeing some deprecation notices, but no errors. Maybe you're seeing an error?

@sheodox
Copy link
Owner

sheodox commented Feb 12, 2022

Actually, before you do that, can you edit some of Anki-Connect's python script?

Find the main script for Anki-Connect, which I believe should be in %appdata%/Anki2/addons21/2055492159/__init__.py. Open that in a text editor.

  1. With all the imports at the top of the file add import traceback.
  2. Find the function def addNotes(self, notes) (note it's addNotes not addNote). You're looking for the except block in that function, like this: https://github.com/FooSoft/anki-connect/blob/7136a15ade29690eb50eb923949db1b60a5e5cd0/plugin/__init__.py#L1649
  3. Before the line that says results.append(None) add:
print('addNotes exception')
traceback.print_exc()

Remember it's python and whitespace matters so those two lines need to be indented to the same level as results.append(None).

Then launch Anki from the command line and see what errors are getting logged when you add cards from Context.Reviews. I have a hunch there's some error that's being encountered and Anki-Connect is just ignoring it. That looks like the only way you'd be seeing results:[null]

@devdouglasonofre
Copy link
Author

devdouglasonofre commented Feb 13, 2022

Bingo.

Error
An error occurred. Please start Anki while holding down the shift key, which will temporarily disable the add-ons you have installed.
If the issue only occurs when add-ons are enabled, please use the Tools > Add-ons menu item to disable some add-ons and restart Anki, repeating until you discover the add-on that is causing the problem.
When you've discovered the add-on that is causing the problem, please report the issue on the add-on support site.
Debug info:
Anki 2.1.49 (dc80804a) Python 3.8.6 Qt 5.14.2 PyQt 5.14.2
Platform: Windows 10
Flags: frz=True ao=True sv=2
Add-ons, last update check: 2022-02-13 10:35:40

Traceback (most recent call last):
  File "C:\Users\Douglas\AppData\Roaming\Anki2\addons21\2055492159\__init__.py", line 1648, in addNotes
    results.append(self.addNote(note))
  File "C:\Users\Douglas\AppData\Roaming\Anki2\addons21\2055492159\util.py", line 46, in <lambda>
    method = lambda *args, **kwargs: func(*args, **kwargs)
  File "C:\Users\Douglas\AppData\Roaming\Anki2\addons21\2055492159\__init__.py", line 670, in addNote
    ankiNote = self.createNote(note)
  File "C:\Users\Douglas\AppData\Roaming\Anki2\addons21\2055492159\__init__.py", line 213, in createNote
    raise Exception('model was not found: {}'.format(note['modelName']))
Exception: model was not found: Basic

After changing the Card Type "Básico" (in portuguese) to "Basic", got the following exception:

Error
An error occurred. Please start Anki while holding down the shift key, which will temporarily disable the add-ons you have installed.
If the issue only occurs when add-ons are enabled, please use the Tools > Add-ons menu item to disable some add-ons and restart Anki, repeating until you discover the add-on that is causing the problem.
When you've discovered the add-on that is causing the problem, please report the issue on the add-on support site.
Debug info:
Anki 2.1.49 (dc80804a) Python 3.8.6 Qt 5.14.2 PyQt 5.14.2
Platform: Windows 10
Flags: frz=True ao=True sv=2
Add-ons, last update check: 2022-02-13 10:35:40

Traceback (most recent call last):
  File "C:\Users\Douglas\AppData\Roaming\Anki2\addons21\2055492159\__init__.py", line 1648, in addNotes
    results.append(self.addNote(note))
  File "C:\Users\Douglas\AppData\Roaming\Anki2\addons21\2055492159\util.py", line 46, in <lambda>
    method = lambda *args, **kwargs: func(*args, **kwargs)
  File "C:\Users\Douglas\AppData\Roaming\Anki2\addons21\2055492159\__init__.py", line 670, in addNote
    ankiNote = self.createNote(note)
  File "C:\Users\Douglas\AppData\Roaming\Anki2\addons21\2055492159\__init__.py", line 268, in createNote
    raise Exception('cannot create note because it is empty')
Exception: cannot create note because it is empty

Then, after some searching, I came across this issue on AnkiConnect's repo. Then I thought about that may be something related to Anki's localization again, as changing the name of the Card Type solved the first issue, so I went to change the fields name as well.

And...
{result: [1644760838652], error: null}
It worked!

Whoa. Who could imagine that this silent error would be related to Card Type's name and fields localization, huh?

Thanks a lot for your support!

@sheodox
Copy link
Owner

sheodox commented Feb 13, 2022

Oh wow, I didn't realize thank Anki localized the card names and field names, that sounds very error prone!

Thanks for helping me investigate this, and thanks for your investigation!

So everything is working for you now? If so I'll close this issue.

In the future I want to make a custom card type for Context.Reviews for efficiency, and that'll probably fix this issue so now I have another reason to do that!

sheodox added a commit that referenced this issue Feb 13, 2022
Expecting there to be an existing card model called "Basic" with "Front" and "Back" fields lead to problems. Those names are all localized in other languages which leads to cards silently not being created. To get around that before adding the notes it will check to see if a "Context.Reviews v1" card model exists, and if it doesn't it will create it.

The card model is mostly the same as the "Basic" model, except that the CSS has been extracted and now exists on the model itself. This has some extra benefits: 1. less duplicated data between cards, 2. night mode on AnkiDroid works! AnkiDroid's night mode will try and invert colors on cards unless the card model includes a `night-mode` class in its css. It unfortunately doesn't look at css in the Front field of the card, but it will look at the model so that's not an issue anymore because we're not using Basic anymore. The html still has a lot of duplication between cards, but that can be improved with a version 2 of the card model along with other features I want to add.

The code is set up so the SRSConstructor just has to return a different model name (e.g. "Context.Reviews v2") and it will create that if needed, so further changes to the model only needs to change SRSConstructor.

The CSS still needs to exist sometimes, so it will be included in all previews on the site, and when exporting the manual text file.
@sheodox
Copy link
Owner

sheodox commented Feb 13, 2022

Could you change your Basic card type name back to "Básico", and then try to create cards through Context.Reviews/Anki-Connect again and see if it works? (Remember to refresh the page first if it's open still)

Context.Reviews should now create a Context.Reviews v1 card model when you import cards and use that instead of relying on a "Basic" card model to exist. This should hopefully fix these localization issues, and be a bit more efficient too. Plus the cards will now work with Night Mode on AnkiDroid!

@devdouglasonofre
Copy link
Author

Everything is working fine! Thanks!

@sheodox
Copy link
Owner

sheodox commented Feb 13, 2022

Nice! Thanks for helping debug this issue!

@sheodox sheodox closed this as completed Feb 13, 2022
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

2 participants