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
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
423dc7a
initial use of dollarmath plugin
kashif Nov 22, 2022
5ff39d0
add frontend support
aliabid94 Nov 22, 2022
495b7de
chnages
aliabid94 Nov 22, 2022
fd5de6b
Merge remote-tracking branch 'origin' into latex-support
aliabid94 Nov 28, 2022
3c1a276
changes
aliabid94 Nov 28, 2022
369c5e7
Merge branch 'main' into latex-support
aliabid94 Nov 28, 2022
56b9be5
changes
aliabid94 Nov 28, 2022
94403c3
Merge branch 'latex-support' of https://github.com/kashif/gradio into…
aliabid94 Nov 28, 2022
2e84ae5
changes
aliabid94 Nov 28, 2022
e8f99c4
changes
aliabid94 Nov 28, 2022
8e6f6f4
fix
aliabid94 Nov 28, 2022
6a01f01
added latex to kinematics blocks
abidlabs Nov 28, 2022
3fd58c7
changes
aliabid94 Nov 28, 2022
805c802
Update CHANGELOG.md
aliabid94 Nov 28, 2022
eeab457
Merge branch 'latex-support' of https://github.com/kashif/gradio into…
aliabid94 Nov 28, 2022
0b1ad59
added example to changelog
kashif Nov 28, 2022
c8e0c74
remove param
abidlabs Nov 28, 2022
a08ec4d
Merge branch 'latex-support' of github.com:kashif/gradio into latex-s…
abidlabs Nov 28, 2022
51acfcf
doc fix
aliabid94 Nov 28, 2022
a91c844
fixes
aliabid94 Nov 29, 2022
501bcd6
Merge branch 'latex-support' of https://github.com/kashif/gradio into…
aliabid94 Nov 29, 2022
a9b75d4
latex noteboox fix
aliabid94 Nov 29, 2022
9132733
fix
aliabid94 Nov 29, 2022
c4e221e
Merge branch 'main' into latex-support
aliabid94 Nov 29, 2022
d50b016
changes
aliabid94 Nov 29, 2022
d32809a
Merge branch 'latex-support' of https://github.com/kashif/gradio into…
aliabid94 Nov 29, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion demo/blocks_hello/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ def welcome(name):

with gr.Blocks() as demo:
gr.Markdown(
"""
r"""
# Hello World!
$\frac{\sqrt{x + y}}{4}$
Start typing below to see the output.
""")
inp = gr.Textbox(placeholder="What is your name?")
Expand Down
5 changes: 3 additions & 2 deletions gradio/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import PIL.ImageOps
from ffmpy import FFmpeg
from markdown_it import MarkdownIt
from mdit_py_plugins.dollarmath import dollarmath_plugin
abidlabs marked this conversation as resolved.
Show resolved Hide resolved

from gradio import media_data, processing_utils, utils
from gradio.blocks import Block
Expand Down Expand Up @@ -2500,7 +2501,7 @@ def __process_markdown(cls, data: List[List[Any]], datatype: List[str]):
return data

if cls.markdown_parser is None:
cls.markdown_parser = MarkdownIt().enable("table")
cls.markdown_parser = MarkdownIt().use(dollarmath_plugin).enable("table")
aliabid94 marked this conversation as resolved.
Show resolved Hide resolved

for i in range(len(data)):
for j in range(len(data[i])):
Expand Down Expand Up @@ -3921,7 +3922,7 @@ def __init__(
visible: If False, component will be hidden.
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
"""
self.md = MarkdownIt().enable("table")
self.md = MarkdownIt().use(dollarmath_plugin).enable("table")
IOComponent.__init__(
self, visible=visible, elem_id=elem_id, value=value, **kwargs
)
Expand Down
2 changes: 2 additions & 0 deletions gradio/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from typing import TYPE_CHECKING, Any, Callable, List, Optional

from markdown_it import MarkdownIt
from mdit_py_plugins.dollarmath import dollarmath_plugin
from mdit_py_plugins.footnote import footnote_plugin

from gradio import Examples, interpretation, utils
Expand Down Expand Up @@ -309,6 +310,7 @@ def clean_html(raw_html):
"html": True,
},
)
.use(dollarmath_plugin)
.use(footnote_plugin)
.enable("table")
)
Expand Down
17 changes: 16 additions & 1 deletion ui/packages/markdown/src/Markdown.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
<script lang="ts">
import { createEventDispatcher } from "svelte";
import "./typography.css";

export let elem_id: string = "";
export let visible: boolean = true;
export let value: string;
export let min_height = false;
import { afterUpdate } from "svelte";

const dispatch = createEventDispatcher<{ change: undefined }>();

let target: HTMLElement;
let katex: Module | undefined;
afterUpdate(async () => {
let latex_elements = target.querySelectorAll(".math");
if (latex_elements.length === 0) {
return;
}

if (katex === undefined) {
katex = await import("./katex");
}
katex.render(latex_elements);
});
aliabid94 marked this conversation as resolved.
Show resolved Hide resolved

$: value, dispatch("change");
</script>

Expand All @@ -18,6 +32,7 @@
class="output-markdown gr-prose"
class:hidden={!visible}
style="max-width: 100%"
bind:this={target}
>
{@html value}
</div>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions ui/packages/markdown/src/katex/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import katex from "katex";
import "./katex.min.css";

export function render(elements: Array<HTMLElement>) {
elements.forEach((element) => {
katex.render(element.innerText, element);
});
}
1 change: 1 addition & 0 deletions ui/packages/markdown/src/katex/katex.min.css

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions ui/packages/markdown/src/katex/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions ui/packages/markdown/src/katex/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "katex",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"katex": "^0.16.3"
}
}