I’m trying to create a button in Anki directly on the Card that, when clicked, buries the current card. Here’s what I have so far:
HTML/JavaScript (in card front):
<button id="buryButton" onclick="custom_bury_card()">Bury</button>
<script>
function custom_bury_card() {
pycmd('custom_bury_card');
}
</script>
Python Add-on (__init__.py
):
from aqt import mw, gui_hooks
from aqt.utils import tooltip
# Function to bury the current card
def custom_bury_card():
card = mw.reviewer.card
if card:
mw.col.sched.buryCards([card.id])
card.flush()
mw.reset()
mw.reviewer.card = card # Update the current card in the reviewer
tooltip("Card was buried.") # Show a tooltip message
# Function to handle the JavaScript message
def on_webview_did_receive_js_message(handled, message, context):
if message == "custom_bury_card":
custom_bury_card()
return (True, None) # Indicate success with (True, None)
return handled # Return the original handled value otherwise
# Register hook to handle JavaScript messages
gui_hooks.webview_did_receive_js_message.append(on_webview_did_receive_js_message)
Despite setting up the button and the Python add-on to handle the custom_bury_card
command, clicking the button does not bury the current card as expected.
I also tried to create a button in Anki that, when clicked, suspends the current card.
<button id="suspendButton" onclick="custom_suspend_card()">Suspend</button>
<script>
function custom_suspend_card() {
pycmd('custom_suspend_card');
}
</script>
Python Add-on (__init__.py
):
from aqt import mw, gui_hooks
from aqt.utils import tooltip
# Function to suspend the current card
def custom_suspend_card():
card = mw.reviewer.card
if card:
mw.col.sched.suspendCards([card.id])
card.flush()
mw.reset()
mw.reviewer.card = card # Update the current card in the reviewer
tooltip("Card was suspended.") # Show a tooltip message
# Function to handle the JavaScript message
def on_webview_did_receive_js_message(handled, message, context):
if message == "custom_suspend_card":
custom_suspend_card()
return (True, None) # Indicate success with (True, None)
return handled # Return the original handled value otherwise
# Register hook to handle JavaScript messages
gui_hooks.webview_did_receive_js_message.append(on_webview_did_receive_js_message)
I would appreciate any guidance to make this button function correctly.
Thank you!