From: Agnibho Mondal Date: Mon, 13 Nov 2023 14:33:45 +0000 (+0530) Subject: Send prescription/data to web app. X-Git-Tag: v0.5~8 X-Git-Url: https://code.agnibho.com/repo?a=commitdiff_plain;h=bc757dbea0f175e420ca8311268a3c239b2abd1c;p=medscript.git Send prescription/data to web app. --- diff --git a/README b/README index 19eb119..2968a30 100644 --- a/README +++ b/README @@ -55,7 +55,7 @@ configurable. An example directory structure is given below: |-- preset (for storing preset files) | |-- additional.csv | |-- advice.csv - | |-- certify.csv + | |-- certificate.csv | |-- investigation.csv | |-- medication.csv | |-- note.csv @@ -426,12 +426,18 @@ An example plugin, that modifies the name of the patient, may be as follows: Plugins may also contain a web app written in HTML/JavaScript. To use it the plugin must contain a function called `web` which takes the prescription object -and returns the URL of the web app. The run function of such a plugin must +and returns a tuple containing the URL of the web app and a json string +containing any data to be passed on to the web app. The `run` function of such a plugin must take two arguments, the prescription and the data returned from the web app. -The web app itself must include the qtwebchannel.js from PyQt6. The webchannel -exposes an object named `js` to the web app. The web app may return data by -calling the `run` function of the exposed `js` object. +The web app itself must include the `qtwebchannel.js` from PyQt6 +(qrc:///qtwebchannel/qwebchannel.js). The webchannel exposes an object named +`js` to the web app. The web app may access the prescription by using the +`js.getPrescription()` and the data from the plugin by using the `js.getData()` +function. The webapp may return data to the plugin by calling the `run` +function of the exposed `js` object with two arguments, first being a json +string containing the prescription object and the second being any data as +string to be passed onto the plugin. Template Development -------------------- diff --git a/plugin.py b/plugin.py index 44a100c..4bb32f2 100644 --- a/plugin.py +++ b/plugin.py @@ -5,7 +5,7 @@ # MedScript is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. # You should have received a copy of the GNU General Public License along with MedScript. If not, see . -import logging, os, importlib, copy +import logging, os, importlib, copy, json from PyQt6.QtWidgets import QMessageBox, QMainWindow, QInputDialog, QFileDialog, QVBoxLayout from PyQt6.QtCore import QObject, QThread, QUrl, pyqtSignal, pyqtSlot from PyQt6.QtWebChannel import QWebChannel @@ -105,8 +105,8 @@ class Plugin(QObject): if(hasattr(module, "web") and callable(module.web)): self.webapp=WebApp() self.webapp.done.connect(lambda: self.update.emit()) - url=module.web(prescription) - self.webapp.load(module, QUrl(url), prescription) + url, data=module.web(prescription) + self.webapp.load(module, QUrl(url), prescription, data) self.webapp.show() elif(hasattr(module, "run") and callable(module.run)): if(hasattr(module, "confirm") and module.confirm): @@ -161,11 +161,11 @@ class WebApp(QMainWindow): self.webview=QWebEngineView() self.setCentralWidget(self.webview) - def load(self, module, url, prescription): + def load(self, module, url, prescription, data): self.module=module self.webview.load(url) self.channel=QWebChannel() - self.js=JS(module, prescription) + self.js=JS(module, prescription, data) self.channel.registerObject("js", self.js) self.webview.page().setWebChannel(self.channel) self.js.done.connect(lambda: self.done.emit()) @@ -174,15 +174,17 @@ class JS(QObject): done=pyqtSignal() - def __init__(self, module, prescription): + def __init__(self, module, prescription, data): super().__init__() self.module=module self.prescription=prescription + self.data=data - @pyqtSlot(str) - def run(self, data): + @pyqtSlot(str, str) + def run(self, prescription, result): + self.prescription.set_data_from_json(json.loads(prescription)) try: - message=self.module.run(self.prescription, data) + message=self.module.run(self.prescription, result) if(message): QMessageBox.information(None, "Information", message) self.done.emit() @@ -190,6 +192,14 @@ class JS(QObject): logging.error(self.module) logging.exception(e) + @pyqtSlot(result=str) + def getPrescription(self): + return self.prescription.get_json() + + @pyqtSlot(result=str) + def getData(self): + return self.data + class Worker(QThread): pluginComplete=pyqtSignal(str, int)