Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Latex support #2696

Merged
merged 26 commits into from
Nov 29, 2022
Merged

Latex support #2696

merged 26 commits into from
Nov 29, 2022

Conversation

kashif
Copy link
Contributor

@kashif kashif commented Nov 22, 2022

Description

Started to add math support for markdown

Closes: #538

Checklist:

  • I have performed a self-review of my own code
  • I have added a short summary of my change to the CHANGELOG.md
  • My code follows the style guidelines of this project
  • I have commented my code in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

A note about the CHANGELOG

Hello 👋 and thank you for contributing to Gradio!

All pull requests must update the change log located in CHANGELOG.md, unless the pull request is labeled with the "no-changelog-update" label.

Please add a brief summary of the change to the Upcoming Release > Full Changelog section of the CHANGELOG.md file and include
a link to the PR (formatted in markdown) and a link to your github profile (if you like). For example, "* Added a cool new feature by [@myusername](link-to-your-github-profile) in [PR 11111](https://github.com/gradio-app/gradio/pull/11111)".

If you would like to elaborate on your change further, feel free to include a longer explanation in the other sections.
If you would like an image/gif/video showcasing your feature, it may be best to edit the CHANGELOG file using the
GitHub web UI since that lets you upload files directly via drag-and-drop.

pngwn
pngwn previously requested changes Nov 24, 2022
Copy link
Member

@pngwn pngwn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to vendor in the css and fonts here, that will be difficult to maintain. We can load them directly from the node package via katex/path/to/assets. Vite can handle css and fonts.

gradio/components.py Outdated Show resolved Hide resolved
ui/packages/markdown/src/Markdown.svelte Outdated Show resolved Hide resolved
ui/packages/markdown/src/katex/package-lock.json Outdated Show resolved Hide resolved
@aliabid94
Copy link
Collaborator

aliabid94 commented Nov 28, 2022

I've implemented support for rendering mathtext serverside, a subset of latex that ships with matplotlib. Screenshot from demo/latex/run.py

Screen Shot 2022-11-28 at 12 54 48 PM

corresponding code:

    gr.Markdown(
    r"""
    # Hello World! $\frac{\sqrt{x + y}}{4}$ is today's lesson

    ## the $\sqrt{x + y}$ is first

    Start with $\frac{\frac{x+1}{x+2}}{x+3}$ then we get $x+2$
    
    There are three formulas to know:
    
    the first is $\gamma^2 + \theta^2 = \omega^2$
    
    $\sqrt{x^2+1}$ is next
    
    Integral $\int_{a}^{b} x^2 \,dx$ is last

    Start typing below to see the output.
    """)

@kashif
Copy link
Contributor Author

kashif commented Nov 28, 2022

nice!

@aliabid94 aliabid94 requested a review from pngwn November 28, 2022 20:06
@github-actions
Copy link
Contributor

The demo notebooks don't match the run.py files. Please run this command from the root of the repo and then commit the changes:

pip install nbformat && cd demo && python generate_notebooks.py

@github-actions
Copy link
Contributor

The demo notebooks don't match the run.py files. Please run this command from the root of the repo and then commit the changes:

pip install nbformat && cd demo && python generate_notebooks.py

@github-actions
Copy link
Contributor

The demo notebooks don't match the run.py files. Please run this command from the root of the repo and then commit the changes:

pip install nbformat && cd demo && python generate_notebooks.py

@abidlabs
Copy link
Member

Very nice. I added some LaTeX to demo/blocks_kinematics and it looks great!

image

Two nits:

  • Let's add documentation for this (maybe in the docstring for Markdown component) as well as tests to prevent regressions
  • I get this warning when I include LaTeX:
savefig() got unexpected keyword argument "frameon" which is no longer supported as of 3.3 and will become an error two minor releases later

You might have to set facecolor='white', transparent=False instead according to this answer: https://stackoverflow.com/questions/59204749/matplotlib-savefig-background-always-transparent

Copy link
Member

@abidlabs abidlabs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM besides a couple of nits (see above)

@kashif
Copy link
Contributor Author

kashif commented Nov 28, 2022

@abidlabs and how does it look in "dark mode"?

@abidlabs
Copy link
Member

@kashif good question -- it looks good to me:

image

CHANGELOG.md Outdated Show resolved Hide resolved
@aliabid94
Copy link
Collaborator

also there is a hidden formula span, so if you try to copy text with latex, it will render the formula.

aliabid94 and others added 2 commits November 28, 2022 13:46
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
@aliabid94
Copy link
Collaborator

@abidlabs I am not able to reproduce your warning - can you check if your suggestion works?

@abidlabs
Copy link
Member

@abidlabs I am not able to reproduce your warning - can you check if your suggestion works?

I upgraded to the latest version of matplotlib and it is indeed throwing an error! Sure I'll see if I can fix it on my end

@abidlabs
Copy link
Member

Ok so I just removed the frameon parameter and it looks like it's fine now @aliabid94

@abidlabs abidlabs dismissed pngwn’s stale review November 28, 2022 20:56

Now the Latex is rendered server-side so this concern is no longer relevant

@github-actions
Copy link
Contributor

The demo notebooks don't match the run.py files. Please run this command from the root of the repo and then commit the changes:

pip install nbformat && cd demo && python generate_notebooks.py

@aliabid94 aliabid94 merged commit d79039b into gradio-app:main Nov 29, 2022
@kashif kashif deleted the latex-support branch November 29, 2022 19:26
@kashif
Copy link
Contributor Author

kashif commented Nov 29, 2022

awesome! thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add LaTeX support for Markdown in description and article
4 participants