GSoC 2016 Application Aman Deep: Implementing a Formula input widget for Sympy in Jupyter Ipython Notebook - wrat/sympy GitHub Wiki
Implementing a Formula input widget for Sympy in Jupyter/Ipython Notebook
Personal Details
Name: Aman Deep
College: National Institute of Technology, Durgapur
Email : [email protected]
Github username: hiamandeep
Blog: www.techinfected.net
My Google Groups Idea Discussion Thread: https://groups.google.com/forum/#!topic/sympy/28cWReuChdI
##Short Biography
I am from India, Currently in Second year BTech, studying Information Technology at National Institute of Technology, Durgapur. I have done 5 tasks in Google Code-In 2011. I love working on Ideas. I also write blog on Linux/FOSS (www.techinfected.net).
Apart from coding, I love music, Especially Classic Rock. I also play Guitar.
##Platform & Editor Experience
I use Ubuntu 15.10 as my primary operating system, I also use windows.
I use SublimeText 3 for large projects and vim for small programs.
I have intermediate expertise in vim.
##Programming Experience
I know C and Python Programming Language.
In web,
I know Javascript, JQuery, Html, Css, Bootstrap, Django, Mysql.
I have built a website for my club using Django and Bootstrap along with a senior. Live at www.onlinetreasurehunt.herokuapp.com hosted on heroku.
Github repo: https://github.com/hiamandeep/online_treasure_hunt_sammita
I have been using Git and Github for some time. I am acquainted with them.
##My Experience with Python
I liked the python language ever since I first used it. I love it for the readability that it provides. I like how the code is written in less number of lines.
My Projects in Python
- A Basic Gui Calculator in Python Using Tkinter. Github link
I have also written a blog post describing, usage and how I wrote the code Link
-
A Basic Music Player written in Python Using Gstreamer. Github Repo
-
A keylogger for linux written in Python using pyxhook module. Github Repo
I have also written an article on my blog showing usage and explaining code Link
##The Project Jupyter does not have a user friendly formula input box to get corresponding sympy expressions. It would be really great if one is made. It will increase user experience to a great extent.
Aim: To create a formula editing widget in Jupyter/Ipython Notebook
The Project can be divided into 2 major parts:
- Implementing a MathQuill widget in Jupyter Notebook .
- Conversion of mathquill expression to python.
###Part 1:
I have used MathQuill for entering and editing the formula, which will then be converted into corresponding Sympy Expression.
MathQuill is a WYSIWYG Latex Math Editor. It uses Mozilla Public License.
The arrow keys or mouse are used to move the cursor in the input box.
For entering Math Symbols, MathQuill takes input in latex. For example: to enter an integration sign in mathquill, the we need to type \int
I have already made a prototype MathQuill Widget in Jupyter. It is working fine.
Here is the link to the code: http://nbviewer.jupyter.org/gist/hiamandeep/bdbf23e83a554d643371
Note: to run the notebook, you will need to download my file from the link and run it locally (First, install Jupyter ) or run it on https://try.jupyter.org/
I have added buttons to the widget for symbols so that the user doesn’t have to know the latex commands. When the user clicks on the button, the sign is inserted into the current cursor position and user can move around the area to fill values in different cells, for example: upper & lower limits in definite integral.
##How I Implemented the Prototype Widget:
First, I hard coded the Html and the Javascript part of the MathQuill Api code as python string in the Jupyter Notebook Cell. After running that cell using Ipython’s HTML function. I was able to produce MathQuill MathField box inside the Notebook.
Here is the code: http://nbviewer.jupyter.org/gist/hiamandeep/52ccdce87c40fb021048
But this was just a page being rendered in Jupyter, I had to make a widget out of it.
So, I went through Ipython’s Documentation and some online resources and to see how an Ipython widget is made.
An Ipython Widget consists of a Back-end Widget and a Front-end Widget.
The Back-end part is written in python and the front-end in Javascript using Backbone.js, It is an MVC (Model view controller) framework written in Javascript.
Simply passing the string containing the Html & javascript from backend to frontend where it is rendered using JQuery’s html function.
Finally, calling the widget shows the MathQuill Input area inside the Jupyter Notebook.
mathquill=(MyWidget())
mathquill
Here is my code: http://nbviewer.jupyter.org/gist/hiamandeep/e337ca54e6ccf041fe81
Then, I further Improved the MathQuill Html and Javascript code, and added buttons to the widget to make it user friendly.
And finally this is my prototype widget code: http://nbviewer.jupyter.org/gist/hiamandeep/bdbf23e83a554d643371
Further Plans:
Every possible Symbol button will be added. The buttons will be categorized in sections. For example: Numbers, Basic Math, Trigonometry, Calculus and so on. There will be a dropdown menu for accessing the category, hence making it uncluttered. And, the commonly used buttons will also be placed outside.
And of course, the user experience will be improved by designing the interface well.
So, after I design the complete interface, It will be just a matter of replacing my existing html & javascript code with the new complete code.
###Part 2:
MathQuill to Sympy expression
Different Approaches:
####Approach 1:
Get Sympy expression using the latex generated by MathQuill latex method and convert it using the latex2sympy package.
latex2sympy is a software package which accepts a latex input and produces its equivalent sympy expression.
So, I will get latex expression from MathQuill and after entering that expression I get sympy expression.
Sympy2latex works well to a good extent. Though, there are some issues that needs to be fixed.
In fact, I submitted an issue regarding the formatting of exponent and it was fixed. PR #6
Another Issue that I fixed myself was that, MathQuill produces \left
and \right
, if there is brackets in equation, they are actually latex commands to make sure that the brackets are of the same height of the expression in html rendered form.
But this is unnecessary and only creates problem in conversion, so I edited the mathquill.js file and removed the ‘\left’ and ‘\right’ from the return statement in latex method. So, the problem is solved.
Here is the modified mathquill.js: Link
Here is an example to show, what I mean.
For an expression: 1/(a+b)
MathQuill’s latex: \frac{1}{\left(a+b\right)}
Modified MathQuill’s latex: \frac{1}{a+b}
Some examples showing conversions from mathquill latex to sympy.
>>> from process_latex import process_sympy
>>>
>>> process_sympy(r"\frac{d}{dx}x^4")
Derivative(x**4, x)
>>>
>>> process_sympy(r"\int x^2dx")
Integral(x**2, x)
>>>
>>> process_sympy(r"\int2^{(x+3)}dx")
Integral(2**(x + 3), x)
>>>
>>> process_sympy(r" \lim_{x\to4}\frac{2-\sqrt{x}}{4-x}")
Limit((-sqrt(x) + 2)/(-x + 4), x, 4)
Problems with this approach:
The author of this package has not updated his status on whether he would like to move it into sympy PR #1 I have asked the author about any updates on this.
But of course the package can be used without merging it into sympy. So, I will have to learn very well, how the latex2sympy source code is written.
There is no documentation about the source code available as of now.
Since This package depends on Antlr4, I will have to learn that.
Advantage with this approach:
I have tried this parser with many different expressions and it works well. Though, I also found some issues.
I won’t have to start from scratch.
####Approach 2:
To find out the data structure that MathQuill uses to store the javascript expression tree and then traversing the expression tree in python with some traversal algorithm should convert the expression into a python expression.
####Approach 3:
Conversion of the latex to Content MathML using SnuggleTex, and then converting Content MathML to Sympy expression.
SnuggleTex has only few content mathml features.
And I also couldn’t find anything other than SnuggleTex for conversion to Content MathML
So there is very less chance that I will go with this approach.
##How Do I fit in
Working on Gui for sympy was my idea and with the help of my mentors, I was shown where and exactly what I should be working on, and I really liked it. I love working on user interfaces. It really excites me that lots of people will be using what I with the help of the opensource community will create. I will be making things lot simpler to many people. I have already made some prototype. I know exactly what to do. I may face problems sometimes but I alway try and try and ultimately I fix the problem.
Work that I have done so far.
-
Made an Interactive Plot in Jupyter Notebook. (Not much related to current idea)
-
Made A basic Prototype MathQuill Widget in Jupyter Notebook.
In the Process I learnt some basics of Backbone.js (an MVC in Javascript) which is used to make widgets in Jupyter/Ipython Notebook.
##Patches Submitted
- (Open) PR #10869 Added an example of an interactive plot using matplotlib in Jupyter Notebook.
- (Open) PR #10871 Made code simpler and fixed compatibility issue with python3
- (Open) PR #10872 Fixed python2 Compatibility
- (Merged) PR #10797 Placed import statement at top to maintain consistency and readability.
- (Merged) PR #10838 Fixed two typos in enumerative.py
- (Open) PR #10819 Fixed missing codes readme.rst
- (Open) PR #10765 Updated readme.rst
##Bugs/Issues raised
- (Closed) PR #6 Exponent Problem.
- (Open) PR #7 Fix undefined symbols problems.
- (Open) PR #8 Improve Integration
##Commitment
I will be giving 50 hours a week for my GSoC project.
I don’t have any other plans.
Even after the Summer of code end, I will still work on my project. Since it will be just the beginning of the new semester so there won’t be much to study.
Schedule
Community Bonding Period (22nd April to 22nd May ):
My Semester exams will end on 6th May. So, I can start working after 6th May.
1st half of the coding period ( 23rd May - 28th June ) :
First of all, I will be working on the conversion of Mathquill expression to sympy, Since It is the most important and challenging part of the project, Then Completing the Widget.
If I go with Approach 1:
-
I will study the grammar, file PS.g4 (Written in Antlr4), and understand how the lexing is done i.e. how the expression is broken down into sub-expression and matched with the lexer rules and when the lexing is complete how the parsing is done.
-
Study the process_latex.py file, which tells antlr to lex the latex expression, parse it and walk it and attach listener to it. (the listener listens for a parser rule which gets triggered. )
( 28th June to 16 August ):
Improving the the widget.
I will be writing blog describing the progress that I make with the project. In fact I love writing blog. I own one already (www.techinfected.net) and I have written 37 posts on technical topics.
##References