MobileRead Forums

MobileRead Forums (https://www.mobileread.com/forums/index.php)
-   Plugins (https://www.mobileread.com/forums/forumdisplay.php?f=268)
-   -   Error on using numpy package into plugin (https://www.mobileread.com/forums/showthread.php?t=314405)

nirosan 01-15-2019 09:15 AM

Error on using numpy package into plugin
 
Hi,

I am writing a plugin which use numpy python package.
But everytime I got this error:

Quote:

Error:
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control). Otherwise reinstall numpy.

Original error was: cannot import name 'multiarray'
I have googled but did not find a suitable solution.

Has anyone an idea or help me please?

Thank you

DiapDealer 01-15-2019 11:45 AM

Numpy is not part of Sigil's bundled python environment. So any plugin wanting to use it would need to include it with the plugin (no idea how one might pull that off), or use an external python environment for Sigil plugins that already had numpy properly installed. Properly installing numpy in an external python environment would likely be a numpy question rather than a Sigil one.

The real question is: what do you need to do to an epub that could possibly require a scientific computing module like NumPy?

nirosan 01-16-2019 06:19 AM

Thank you.
I had include numpy package and other required packages into my plugin package but got the same error. If I use local python interpreter then its works. But the packages whick I inlcuded are from the same python directory. The user of the plugin should be able to use it with the sigil bundled python and included packages. Therefore I need a solution.

The plugin which I write is to render Latex math formulas notation into mathml. To display the rendered formulas with symbols I need matplotlib package and matplotlib need numpy.

DiapDealer 01-16-2019 07:38 AM

I'm assuming you're creating an input plugin then? Otherwise, a sigil plugin won't help you render/display anything.

As to how to package numpy/matplotlib to work in Sigil's bundled python environment, I'm afraid I can't help. I suspect that would be relatively complicated (not to mention large) to accomplish. Pure Python modules are fairly easy to relocate/include. Binary packages can be quite involved and include hardcoded paths or environment variables necessary to function properly.

Also note that it's not likely your plugin would work on all Sigil platforms (Windows, OSX, Linux). If installed via pip or your package manager (or other binary media), your compiled modules are going to be platform specific. The binary wheels are also usually specific to a particular Python version. So even if you manage to get something working with Sigil's bundled Python, it's likely to break when we upgrade the version of python that we bundle.

For binary dependencies, it's simply going to be best to use an external, python interpreter to run the plugin. That way, you can have your users install the binary dependencies.

nirosan 01-16-2019 08:23 AM

Thank you for your quick reply.

Yes is it an input plugin. I am trying some solutions but not works. Another way is to display on QWebEngineView. Is QtWebEngineWidgets part of sigil bundled python environment? May be not I got this error:

Quote:

from PyQt5.QtWebEngineWidgets import QWebEngineView
ModuleNotFoundError: No module named 'PyQt5.QtWebEngineWidgets'
Error: No module named 'PyQt5.QtWebEngineWidgets'
How can I include QWebEngineWidgets in my plugin?

Thank you

DiapDealer 01-16-2019 09:39 AM

Quote:

Originally Posted by nirosan (Post 3799738)
Thank you for your quick reply.

Yes is it an input plugin. I am trying some solutions but not works. Another way is to display on QWebEngineView. Is QtWebEngineWidgets part of sigil bundled python environment?

No, it is not part of the PyQt5 module we include with our bundled Python. Worse than that, Sigil doesn't include QWebEngine(View) with its Qt5. Sigil only utilizes QtWebKit (and no, we don't include the PyQt5 interface module to QtWebKit either).

Sigil's plugin framework was never intended to be able to do a lot of high-level rendering. We've only included enough modules for people to do some basic Widgets (PyQt and Tkinter) to utilize in gui interfaces intended to get user input for their plugins' parameters.

Quote:

Originally Posted by nirosan (Post 3799738)
How can I include QWebEngineWidgets in my plugin?

To put it quite bluntly: you can't. Sorry. Sigil plugins were not intended to be used to display content. They're intended to import/edit/validate/export content.

Doitsu 01-16-2019 10:49 AM

Quote:

Originally Posted by nirosan (Post 3799720)
To display the rendered formulas with symbols I need matplotlib package and matplotlib need numpy.

BTW, Sigil (and some epub3 apps) display MathML formulas in epub3 books without conversion. I.e., you won't have to convert MathML formulas, unless you limit yourself to epub2 books. (For MathML examples, see EPUBTEST 0100 - Reflowable Content Tests.)

IIRC, there are also some Python packages and standalone PostScript/LaTex/MathML to SVG converters that you could use, if you don't want to support epub3 books. (Many epub2 apps support SVG images.)

nirosan 01-19-2019 04:31 AM

Thank you

Yes It is better to transfer mathml into svg and display. Now I am searching for a python module for that.
Thank you for your quick replies.

Tex2002ans 01-19-2019 12:44 PM

Quote:

Originally Posted by nirosan (Post 3799720)
The plugin which I write is to render Latex math formulas notation into mathml. [...] Now I am searching for a python module for that.

plastex could potentially be a solution. It's written in Python, and does TeX -> HTML.

There's also these two answers on the LaTeX Stack Exchange that cover some other tools:

https://tex.stackexchange.com/questi...mathml-and-tex
https://tex.stackexchange.com/questi...and-approaches

(tex4ht converts LaTeX -> HTML, and very customizable.)

Quote:

Originally Posted by nirosan (Post 3799720)
Yes It is better to transfer mathml into svg and display.

You could use some of the above tools to LaTeX -> MathML, and then go MathML -> SVG+PNG.

Or I personally go LaTeX -> PDF -> SVG+PNG.

I wrote about that most recently in this topic. (Also, if you check out the ebookcraft 2018 "Equations in Ebooks" slides, you may get some more methods/ideas too.)

If you're putting this book up for sale on the major retailers, you have to keep in mind the millions of older devices out there that can't read/render MathML, so you must design these image fallbacks anyway. Can't rely on purely MathML, which is a shame.

nirosan 01-22-2019 09:25 AM

Thank you.

I already use some python modules to convert latex and asciimath into mathml and it works fine. (Modules: latex2mathml and asciitomathml).

What I need is a python module to convert the mathml into svg or any images to display the rendered mathml. The user should be able to see the rendered formula before she/he can add it into the book.

What I had used is with matplotlib module:

Quote:

dpi = 300
fig = plt.figure(figsize=(0.01, 0.01))
fig.text(0, 0, r'${}$'.format(self.formulas_input.text()), fontsize=fontsize)

output = BytesIO()
fig.savefig(output, dpi=dpi, transparent=True, format='svg',
bbox_inches='tight', pad_inches=0.0, frameon=False)
plt.close(fig)

output.seek(0)
svg_output = output.read()
self.render_svg.load(svg_output)
The problem was matplotlib use numpy.

I did not find a suitable python module to write mathml into svg which can I load into QSvg to display it.

nirosan 01-29-2019 06:25 AM

Thank you

I found a workaround:

- transform latex formulas into png

Quote:

...
requests.get('http://latex.codecogs.com/png.latex?\dpi{{300}} {formula}'.format(formula=formula))
...
- load png into QPixmap to display it on the editor


All times are GMT -4. The time now is 08:57 PM.

Powered by: vBulletin
Copyright ©2000 - 3.8.5, Jelsoft Enterprises Ltd.
MobileRead.com is a privately owned, operated and funded community.