How to suppress graphviz warnings in Python 3? - python-3.x

I'm using the graphviz module to render a network of nodes and links.
I use small circles as node's shape, so the labels are intentionally bigger than the nodes.
As a consequence, I get the following warning:
"Warning: node 'wave', graph 'N' size too small for label"
'Wave' is just an example of a node's label.
I get lots of this warnings because of the high quantity of nodes (screencapture).
So, my question is: How can I suppress warnings like those?
The graphviz command I'm using is:
n.view() # n is my digraph
I have already tried the suggestions from:
How to suppress a third-party warning using warnings.filterwarnings
How to redirect python warnings to a custom stream?
But nothing so far.
Thanks in advance.

Try Eli Bendersky's excellent page: Redirecting all kinds of stdout in Python
After replacing stdout with stderr, Eli's solution worked on graphviz for me.
If you happen to be using Evince, Ubuntu's builtin PDF viewer, see:
https://superuser.com/questions/980237/silence-evinces-warnings-in-ubuntu

For graphviz, as of v0.11, there is a silent option for the .draw() method (see graphviz docs:graphviz.view).
Apparently not on windows.
This applies to those using networkx (which uses pygraphviz) or pygraphviz.
In pygraphviz, the warnings are collected and then pushed using the warnings module (see pygraphviz/agraph.py:1390).
You can therefore silent the warnings specifically when drawing:
warnings docs:Temporarily Suppressing Warnings
import warnings
<create graph etc>
with warnings.catch_warnings():
warnings.simplefilter("ignore")
g.draw()

Related

Spyder - disable warnings inside Spyder Console

I use Spyder (4.1.5) as my python editor. When I run the code it shows a million workings that make it impossible for me to really focus on the output of the code. I read a lot of posts here on stackoverflow (see for example: Hide all warnings in ipython ) and it seems they all suggest to add
warnings.filterwarnings("ignore")
This for sure does not work.
For example, one of the many warnings printed to console many times is this one:
C:\...\Utils.py:2831: FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be deprecated, use a list instead.
However, I also have this line of code at the very beginning of the script:
warnings.filterwarnings("ignore", category=FutureWarning)
Still, no luck in disabling it.
import warnings
warnings.filterwarnings("ignore")
worked for me using Spyder 5 and Python 3.8.10

Pyside to build PyQT designer 4 UI file always results in error

Using Pyside-uic -o pythonfilename.py uifilename.ui to convert my PyQT4 designer GUI to python code. Seems to build just fine with no errors, but when I run the file, it consistently crashes, with type errors I can't solve.
First error gave me trouble because I set a "max size". When I removed the max size, the error changed to be a problem with the palette. I removed the palette settings, and replaced it with style sheets, and now it's giving me the error:
"TypeError: QWidget.setFont(QFont): argument 1 has unexpected type 'PySide.QtGui.QFont'"
I doubt there are this many problems with using Pyside to build PyQT UI files(because it seems to be a pretty standard way of doing it), so that leaves me to believe it's something i'm doing.
**Just a note with a bit more info.
This is software we've been using for some time, and the existing UI was built using this exact method. I inherited the file, and was asked to update the UI, and left with instructions on how to do it. I updated the existing ui file, saved it out, and ran the "Using Pyside-uic -o pythonfilename.py uifilename.ui" command. No errors from the build, but %100 of the time i've tried using this method, it has failed.
I tried googling the answer for hours, and proposed the question to other people.
I solved the issue. After pyside spits out the new python file (built from the UI file), theres a line of code at the beginning that will say something along the lines of "from PySide import QtCore, QtGui"
"PySide" has to be changed to "PyQt4".

wxPython pdfviewer unknown font preventing pdf loading

While checking out wxPython's wx.lib.pdfViewer, I ran the code in the documentation. It worked well, but most of the pdf did not load, and messages like these
Unknown font ghlcbg+arial
Unknown font ghlcdi+arial,bold
were printed. I surmise that it is because the fonts used in the pdf are not implemented by PyPDF2. How can I solve this problem? I can preprocess the pdf to replace the fonts if necessary, but this I do not know how to do either.
Is there a library which allows for replacement of fonts? If there is not, is there a replacement for PyPDF2 which permits this replacement (PyMuPDF does not work for me due to another error)? If such a replacement is not possible, is there an extension which permits the interpretation of such fonts?
wxPython cannot load a PDF that PyPDF2 does not support. I think there is talk about moving to pdfrw, but that has not occurred as of yet and I don't know if it actually has handling for unknown fonts or not. The main benefit to moving is PyPDF2 is no longer being maintained.
PyQt can do it since it integrates with poppler. See https://github.com/frescobaldi/python-poppler-qt4 or http://shallowsky.com/blog/programming/qt5-poppler-pdf.html

Python ANSI Color Codes

Python 3.7, on Windows print does not work as expected for ANSI color codes until shell=True once in subprocess.call().
In the below links it appears to imply that the ANSI color codes should work using the "print" command out of the box.
How to print colour/color in python?
Print in terminal with colors using Python?
the second one mentions VT100 emulation... not sure what exactly that means. I am able to write a batch file that outputs the color fine so I would think (naively) that it should work the same way in Python.
However I am not able to use the ANSI color codes as it seems that the ESC character is being "commented out"(?) because for instance when I
print(u"\u001b[31mHelloWorld")
I am not able to see the colored output, as the ESC character seems to be necessary in Windows and prints in the python shell as "[?]" (a box with a question mark)
Is there something I am missing here?
I found myself an answer. As often happens I just did not look far enough.
the Colorama module can be installed with
py -m pip install colorama
and comes with a method definition at the root of the module called init
colorama.init()
This is a cross platform function in that it is only useful on windows (it saves the active Terminal state for reversal and writes the Terminal to preprocess ANSI codes), it does nothing for other operating systems.
I am thinking about implementing an even more lightweight solution using ctypes and setting the Interpret flags on the active terminal myself.
If you are interested in more information, see here:
https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
Output Sequences
The following terminal sequences are intercepted by the console host when written into the output stream, if the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag is set on the screen buffer handle using the SetConsoleMode flag. Note that the DISABLE_NEWLINE_AUTO_RETURN flag may also be useful in emulating the cursor positioning and scrolling behavior of other terminal emulators in relation to characters written to the final column in any row.
Emphasis mine.

How do I change LaTeX to MathML using MathJax in Node.js?

I'm trying to render math equations from user input server side using MathJax in Node.js. I'm using a separate library (mathjs) to turn the input into LaTeX, but after spending an hour looking at the MathJax docs I'm no closer to turning the LaTeX into MathML.
I'm assuming I'd be using the mathjax package on NPM, and then use the MathJax.InputJax and MathJax.OutputJax classes to do the processing, but I've got no idea how to put everything together.
(In case anyone is wondering, I'm using a CSS file to polyfill MathML, rather than load MathJax in the browser and cause a delay while the processing happens.)
You want to use MathJax-node for this. It is set up to run MathJax server-side. There are a number of example command-line tools in the bin directory. You could also hook up a web service to handle the conversion. Tim Arnold made one available as mathjax-server on npmjs.com.

Resources