Skip to content
This repository was archived by the owner on Nov 4, 2025. It is now read-only.

Commit 9f7dcca

Browse files
authored
Merge pull request #392 from mikkkee/master
Add a method to bring up Import File dialog
2 parents 6693ab6 + 9e214e9 commit 9f7dcca

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,35 @@ corresponding to when the API was available for use.
16171617
```
16181618
</details>
16191619
1620+
#### `guiImportFile`
1621+
1622+
* Invokes the *Import... (Ctrl+Shift+I)* dialog with an optional file path. Brings up the dialog for user to review the import. Supports all file types that Anki supports. Brings open file dialog if no path is provided. Forward slashes must be used in the path on Windows.
1623+
1624+
<details>
1625+
<summary><i>Sample request:</i></summary>
1626+
1627+
```json
1628+
{
1629+
"action": "guiImportFile",
1630+
"version": 6,
1631+
"params": {
1632+
"path": "C:/Users/Desktop/cards.txt"
1633+
}
1634+
}
1635+
```
1636+
</details>
1637+
1638+
<details>
1639+
<summary><i>Sample result:</i></summary>
1640+
1641+
```json
1642+
{
1643+
"result": null,
1644+
"error": null
1645+
}
1646+
```
1647+
</details>
1648+
16201649
#### `guiExitAnki`
16211650
16221651
* Schedules a request to gracefully close Anki. This operation is asynchronous, so it will return immediately and

plugin/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from anki.importing import AnkiPackageImporter
4242
from anki.notes import Note
4343
from anki.errors import NotFoundError
44+
from aqt.import_export.importing import import_file, prompt_for_file_then_import
4445
from aqt.qt import Qt, QTimer, QMessageBox, QCheckBox
4546

4647
from .web import format_exception_reply, format_success_reply
@@ -1852,6 +1853,32 @@ def guiDeckReview(self, name):
18521853
return False
18531854

18541855

1856+
@util.api()
1857+
def guiImportFile(self, path=None):
1858+
"""
1859+
Open Import File (Ctrl+Shift+I) dialog with provided file path.
1860+
If no path is given, the user will be prompted to select a file.
1861+
1862+
path: string
1863+
import file path, note on Windows you must use forward slashes.
1864+
"""
1865+
1866+
# Bring window to top for user to review import settings.
1867+
try:
1868+
# [Step 1/2] set always on top flag, show window (it stays on top for now)
1869+
self.window().setWindowFlags(self.window().windowFlags() | Qt.WindowStaysOnTopHint)
1870+
self.window().show()
1871+
finally:
1872+
# [Step 2/2] clear always on top flag, show window (it doesn't stay on top anymore)
1873+
self.window().setWindowFlags(self.window().windowFlags() & ~Qt.WindowStaysOnTopHint)
1874+
self.window().show()
1875+
1876+
if path is None:
1877+
prompt_for_file_then_import(self.window())
1878+
else:
1879+
import_file(self.window(), path)
1880+
1881+
18551882
@util.api()
18561883
def guiExitAnki(self):
18571884
timer = QTimer()

tests/test_graphical.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ def test_guiDeckOverview(setup):
2828
assert ac.guiDeckOverview(name="test_deck") is True
2929

3030

31+
def test_guiImportFile(setup):
32+
ac.guiImportFile()
33+
34+
3135
class TestAddCards:
3236
note = {
3337
"deckName": "test_deck",

0 commit comments

Comments
 (0)