2

The below code works fine, but when I use the same code as an Anki addon, BeautifulSoup raises an attribute error.

Here is the working code:

from bs4 import BeautifulSoup

s = '''<style>.card {
 font-family: arial;
 font-size: 20px;
 text-align: center;
 color: black;
 background-color: white;
 }
 </style>question

 <hr id=answer>

 answer''' 

soup = BeautifulSoup(s, "html.parser")
soup.find("style").clear()
words = soup.text.strip().split()
print words

>>> [u'question', u'answer']

Here is the content of the Anki addon file:

from aqt.reviewer import Reviewer
from aqt.editor import *

def newAnswerCard(self, ease):
    "Reschedule card and show next."
    if self.mw.state != "review":
        # showing resetRequired screen; ignore key
        return
    if self.state != "answer":
        return
    if self.mw.col.sched.answerButtons(self.card) < ease:
        return
    if ease == 1:
        s = '''<style>.card {
         font-family: arial;
         font-size: 20px;
         text-align: center;
         color: black;
         background-color: white;
        }
        </style>question

        <hr id=answer>

        answer''' 

        soup = BeautifulSoup(s, "html.parser")
        soup.find("style").clear()
        words = soup.text.strip().split()

    self.mw.col.sched.answerCard(self.card, ease)
    self._answeredIds.append(self.card.id)
    self.mw.autosave()
    self.nextCard()

origAnswerCard = Reviewer._answerCard
Reviewer._answerCard = newAnswerCard

Anki is supposed to run this code upon clicking on the "Again" button. However, it raises this an attribute error:

Traceback (most recent call last):
  File "aqt\webview.py", line 21, in link
  File "aqt\reviewer.py", line 323, in _linkHandler
  File "C:\Users\A\AppData\Roaming\Anki2\addons\amintest2.py", line 27, in newAnswerCard
    soup = BeautifulSoup(s, "html.parser")
  File "thirdparty\BeautifulSoup.py", line 1521, in __init__
  File "thirdparty\BeautifulSoup.py", line 1146, in __init__
  File "thirdparty\BeautifulSoup.py", line 1188, in _feed
  File "sgmllib.py", line 104, in feed
  File "sgmllib.py", line 138, in goahead
  File "sgmllib.py", line 296, in parse_starttag
  File "sgmllib.py", line 338, in finish_starttag
  File "thirdparty\BeautifulSoup.py", line 1343, in unknown_starttag
AttributeError: 'str' object has no attribute 'text'

I suppose I am using bs4since I import from aqt.editor import *. Here is the content of act.editor:

https://github.com/dae/anki/blob/4693e71104dbe7d19a3e7242b0c2e67a1b4107d8/aqt/editor.py

I do this import only because I could not import from bs4 import BeautifulSoupwithin the addon (another confusion of me!).

I am running on a win7 virtualbox which has no python installed, except for the Anki, to be sure that I am using the python environment that comes with Anki installation.

Thanks in advance!

Amin
  • 437
  • 2
  • 4
  • 17
  • It is expecting an object that isn't a string. My _guess_ is that it wants a `BeautifulSoup` instance. Try adding the line `raise Exception(repr(BeautifulSoup) + " " + repr(type(BeautifulSoup)))` and show what it prints - I am guessing that it's something different to the `BeautifulSoup` you have outside Anki. – wizzwizz4 Aug 26 '17 at 15:24

1 Answers1

0

You need a copy of Beautiful Soup 4. Download the latest version here:

http://www.crummy.com/software/BeautifulSoup/bs4/download/

Unzip the code and copy the folder "bs4" to the Anki Add-On folder. Then restart Anki.

Kellen
  • 121
  • 5