Bỏ qua đến nội dung

pyop

Chờ xử lý #anki #addon #pyop
https://forums.ankiweb.net/t/pyop-support-thread/29794
28/4/2023

Cách tải addon pyop

Bạn có thể tải addon bằng một trong hai cách sau:

Click nút Copy bên dưới để copy code vào clipboard

525125636

Sau đó mở Anki → Tools → Add-ons → Get Add-ons → Dán code → OK

Mở trang addon trên AnkiWeb và tìm mã code ở cuối trang

Mở trên AnkiWeb

Cuộn xuống cuối trang AnkiWeb, tìm dòng có mã code 525125636 và copy

Mô tả chi tiết

PYOP

Anki extension to run user supplied python as a background operation with access to selected card (cid), note (nid), deck (did) and collection (col) through pyop module.

Use case

One example would be a quick script to generate notes from a website using BeautifulSoup with a specific deck as destination for the notes.

Example, <addon>/user_files/parse.py:

import re, tempfile, shutil
from urllib import request
from bs4 import BeautifulSoup
from pyop import col, did


URLS = [
("https://something.net/subdir/one/", 10),
...
]




model = col.models.by_name('Flexible Cloze 2 (min)')
opener = request.build_opener()
opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36')]
request.install_opener(opener)
tmpdir = tempfile.mkdtemp()




for (url, n) in URLS:
m = re.match(rf'^.?/subdir/(.?)/.*', url)
title = m.group(1)
html = rf"&lt;h1&gt;&#123;m.group(1).replace('-', '').capitalize()&#125;&lt;/h1&gt;"
cloze = 1
for i in range(1, n+1):
req = request.urlopen(rf'&#123;url&#125;&#123;i&#125;')
rhtml = req.read().decode("utf8")
req.close()
soup = BeautifulSoup(rhtml, 'html.parser')
imgel = soup.select_one('#something-image')
tmpfile =  rf"&#123;tmpdir&#125;/&#123;title&#125;-&#123;i&#125;.&#123;(imgel['src'].rsplit('.', 1))[1]&#125;"
request.urlretrieve(rf'https://something.net&#123;imgel["src"]&#125;', tmpfile)
img = col.media.add_file(tmpfile)
html += rf'&lt;hr&gt;&lt;img src="&#123;img&#125;"&gt;'



    q = []
    a = []
    for itm in soup.select('.something-question'):
        q.append(itm.get_text())
    for itm in soup.select('.something-answer'):
        a.append(itm.get_text())
    for x in range (len(q)):
        html += rf'&lt;br&gt;&lt;br&gt;&#123;q[x]&#125;&lt;br&gt;' + '&#123;&#123;c' + rf'&#123;cloze&#125;::&#123;a[x]&#125;' + '&#125;&#125;'
    cloze += 1

note = col.new_note(model)
note['Text'] = html
col.add_note(note, did)

shutil.rmtree(tmpdir)

Use

The addon adds two context and top bar menu options:

Pyop: Select file: select which file/python module to run. In the module importing pyop will give access to the current selection in the form of col, did, nid and cid. Depending on how the addon is launched (table context menu or main window menu etc.) some of the values may be None. Pyop: Run <file name>: will load the file/module from disk (and hence run the code). The module is loaded from disk every time (to avoid having to restart Anki when debugging the script).

It may be worth running Anki from the console so that you have access to standard output while testing your scripts. Storage of the user scripts can be anywhere but <addon>/user_files is recommended.


Liên kết hỗ trợ