PyQt: how to show math equations in QTextEdit - python

Merged with How to show math equations in QTextEdit?.
Does anyone know how to show mathematical equations in QTextEdit written in any format (TXT, MathML, MML, LaTeX, HTML, XML or anything) using only Python, that is without using 3rd party packages like Latex itself.? I tried to find the MathML Widget from Qt but it disappeared from their web site and I couldn't find it anywhere.
I'm not fussed about the equation source (either a string or a file). I just can't figure out how to show them nicely (the same way as they appear on a web page, e.g. http://en.wikipedia.org/wiki/MathML).
My QTextEdit currently gets an HTML string. I tried embedding the MathML example from that Wikipedia article, but it doesn't show it properly, it ignores all those MathML tags.
Will really appreciate your help. Thank you.

Related

Inserting comments into jupyter notebook

I hope this is not a redundant question, but I was not able to find a proper answer and tutorial. I am currently learning Data Science and therefore use kaggle competition for practice and jupyter notebook to tackle them (as it is visually more appealing to me). Now I stumbled upon an example kernel (see: http://nbviewer.jupyter.org/github/agconti/kaggle-titanic/blob/master/Titanic.ipynb#Data-Handling) is using comments, links and illustrations between his codes to give more context.
Is anybody aware of how to write such comments and remarks within jupyter? If so, do you have an online resource where I could learn this?
Thanks a bunch!
Yep - highlight a cell and click on the "Cell" dropdown menu and go to "Cell Type" and choose "Markdown". Then you can type any markdown text you'd like and it will render as such.
^^ Also there are shortcuts for changing cell types as well. Highlight a cell and press the esc key to change into "command" mode and press m. This changes it to a markdown cell. Press y to change it back to a code cell.
You can also do latex equations using dollar signs ($). There is documentation for this here
For markdown cells
[//]: # "Your comment in here."
allows to include a comment in jupyter notebook server version 6.0.1. See this discussion for more details on markdown comments.
Agree with #RSHAP. I just add the flow picture in the below.
select the active Cell > Cell Type > Markdown.
irrespective of the version of jupyter notebook you use, the following command should work on current cell
Pres ESC
then Press m
As mentioned in the above remarks, use markdown cells.
Once in a markdown cell, use #,##,###,#### for headings of different fonts. For bold, use **your_text_here**, and for italics use *your_text_here*. For writing equations and using latex commands, use $ symbol. Everything works just like in latex; you can even define your notations like you would normally do in Latex. For a more detailed and structured overview to format text In Jupyter, have a look at this website:
https://www.earthdatascience.org/courses/intro-to-earth-data-science/file-formats/use-text-files/format-text-with-markdown-jupyter-notebook/

Different behavior in powerpoint and libreoffice by ppt generated using python-pptx

I am trying to create a file using python-pptx on a flask server. All of this is working and even the download is working by the problem comes when I try to use text_frame.auto_size in my code. LibreOffice Impress displays the text perfectly but MS Powerpoint does not display the text properly.
Here are the images explaining the issue -
LibreOffice -
Powerpoint -
Also, here is the code that I am using -
text_box = slide.shapes.add_textbox(left, top, width, height)
text_frame = text_box.text_frame
text_frame.word_wrap = True
text_frame.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE
Any idea what I am doing wrong here?
This is unfortunately a limitation of PowerPoint and an unusual (in my experience) place where LibreOffice actually does better.
You'll notice that if you click in the PowerPoint text, insert a space and then delete it, that the text will automatically autofit. This may not fix the problem, but it points to the cause.
In the XML behind the slide, the current "auto-fitted" font size of the textbox is cached. LibreOffice automatically recalculates this cached figure when the presentation opens; PowerPoint does not.
Calculating the "auto-fitted" font size is the job of the rendering engine, which has access to font sizes, line/word breaks, etc. python-pptx does not include a rendering engine nor does it have access to one (none exists for Python as far as I know). So the best it can do is estimate it and it prefers not to do that, since that's getting into rendering.
However, there is an experimental feature in the form of the .fit_text() method that may get you most of the way there. Basically, that capability was so wanted that someone was willing to sponsor a "best-efforts" solution, which is what that method represents. The documentation at that link explains how to use it and its limitations.
Note that that method is experimental, meaning it won't be considered a bug if it doesn't work the way you need it to. You're free to elaborate it if you can do better.

How to display parsed JSON text

Let me start off by saying that I'm starting this project to get familiar with Python. The project is to parse JSON from Reddit.com every 15 minutes and then display it with Tkinter.
Parsing the JSON text wasn't too difficult, but I'm not sure how display the text. I will need to display two things -- the title of the link and the link -- in a column style view. I was also planning on having two columns side by side to take advantage of the horizontal screen width. I've tried starting out with a Listbox, but that seemed to more be used if you want a selectable list. I just want to display the text and be done with it. Is there a different and more appropriate widget I should be using? I looked into the Entry widget a bit and thought that might be getting closer, but that seemed to only be for storing a single entry.
If you are using a sufficiently modern version of python (2.7, 3....something, I forget) you can use the ttk.Treeview widget which is similar to the listbox but supports multiple columns.
A much simpler choice is to use the text widget. You can insert the text followed by a newline. To line thimgs up in columns just insert a tab, and define an appropriate tab stop.
There are plenty of other options, but those two are arguably the best.
If you haven't dicovered it yet, tkdocs.com has a wealth of information about tkinter.

LaTeX type rendering with pygame

I am trying to read text with LaTeX formatting and render it using pygame. I am a novice to pygame and any help will be appreciated.
I looked into Glyph package. The problem is that I would have to rewrite all my .txt files in Glyph formatting for them to be useful. If I try and define macros I would essentially have to build a LaTeX translator. Both these options don't seem very practical considering the number of text files I have.
You could render LaTeX via matplotlib and use these figures in PyGame as sprites.
http://matplotlib.sourceforge.net/users/usetex.html
Check out pylasem (yes, it says MathML rendering library, but it also supports a subset of Latex) or lasem itself via gobject-introspection.

How can I achieve layout similar to Google Image search in QT (PyQT)?

I'm new to QT. I'm using PyQT for GUI development in my project. I want to achieve this layout in my application. This application searches images from an image database. Google image search layout is ideal for my purpose.
I'm following the book "Rapid GUI Programming with Python and Qt" and I'm familiar with layouts. I guess I need to use a grid layout with each result image in each box of grid. & use vertical layout for (image,Qlabel,Qlabel) inside each grid box.
These are some problems I'm facing.
Importantly, I'm unable to display image. What control/widget do I need? I cannot find anything similar to PictureBox of .NET
How do I seperate these image result by fixed gap like in the image? I'm using Horizontal & vertical spacers but that isn't working?
How to set QLabel a clickable (like hyperlink). I don't want to a open a URL. Just the text should be clickable. So, that when user clicks on the link. I can show more information (like next set of results when he clicks on next page number or a new window with image in fullsize when user clicks on 'view') Do we have some new kind of control for this?
This is another important issue. I'll display the page numbers of results (like shown in figure) & assuming they are clickable. How do I load a new page of results? I mean what is the equivalent of page in QT?
As you can guess. This definitely wont be the first page of GUI. The first page will be exactly like http://google.com (a big logo & text box with button below it). when user clicks the search button. This page will be displayed. Again the same question comes up. How change the pages?
Please give a list of controls I'm going to need for this. Tell me if I'm unaware of something.
1/2.
For displaying the images and labels use a QListWidget with view mode set to QListView::IconMode. However, if you need to customize the display beyond what the QListWidget/QListWidgetItem api can provide you will need to create your own QAbstractListModel and use a standard QListView with it.
Make sure and read Qt's primer on model/view.
As for spacing the images, checkout the spacing property on the list view.
Here is an example from KDE's Dolphin file manager:
3. Use a regular QLabel, but set the contents to be an href.
Example:
edit: Oops I see from your tags you are using PyQt, the following is C++, but should be similar to what you would do in python.
QLabel *linkLabel = new QLabel;
linkLabel->setTextFormat( Qt::RichText )
linkLabel->setText( " Click me! " );
connect( linkLabel, SIGNAL( linkActivated ( const QString & link ) ), .... )
4.
Well, since you are using a Model/View, why bother having page numbers at all? The user will just be able to scroll the view and more pictures will be shown. This is by far the easiest solution as you don't have to do anything once you've got your M/V setup!
However, if you really want to show page numbers it will require more work in your model. For example, have a track the "current page" in the model and only allow access to images on the "current page". Then in your slot connected to the linkActivated() signal tell the model to change pages. I won't go into much more detail as this seriously violates the whole idea behind model/view. The "right way" of doing this would be to subclass QListView and add pagination support, but like I said why not use scroll bars? There isn't any performance hits to doing so.
5. Use a QStackedWidget, addWidget() all your "pages" to it, then call setCurrentIndex/Widget() as needed to switch the pages.
Thoughts:
It seems you are very committed to cloning the look, feel, and behavior of Google Image search, which is fine, but Google Image Search is a web application that uses interaction paradigms that are very different than a normal desktop application (links, pages, etc). You are presumably developing a desktop application, and by trying to emulate the behavior of a web app you will find it difficult as the API just isn't designed to support those sorts of interactions. By all means, it is doable, but you'll have your work cut out for you.
If you are extremely intent on sticking to the web based interaction style, why not code your app in javascript and HTML and toss it in a QWebView?
Try using QListWidget with viewMode set to IconMode. It should do all for you. BUT if you need to customize your data display use QListView with your own/standard model and own delegate for painting

Categories