Skip to content

Implement SVG rendering of equations in dcc.Graph and dcc.Markdown using MathJax v3 - #1949

Merged
alexcjohnson merged 47 commits into
devfrom
mathjax-v3
Mar 12, 2022
Merged

Implement SVG rendering of equations in dcc.Graph and dcc.Markdown using MathJax v3#1949
alexcjohnson merged 47 commits into
devfrom
mathjax-v3

Conversation

@archmoj

@archmojarchmoj commented Feb 25, 2022

Copy link
Copy Markdown
Contributor

Also bumping plotly.js to v2.10.1.
See additions and fixes in v2.10.0 and v2.10.1.

Example app:

importdashfromdashimportdccfromdashimporthtmlapp=dash.Dash(__name__)
false=Falsetrue=Truefig= {
"data":[{
"x": [0, 1],
"y": [0, 1.414],
"name": "$E^2=m^2c^4+p^2c^2$"
}, {
"x": [0, 1],
"y": [1.4, 0.1],
"type": "bar",
"name": "$x=\\frac{-b \\pm \\sqrt{b^2-4ac}}{2a}$"
}, {
"type": "pie",
"values": [1, 9],
"labels": ["$\\frac{1}{10}=10\\%$", "$\\frac{9}{10}=90\\%$"],
"domain": {"x": [0.3, 0.75], "y": [0.55, 1]}
}, {
"type": "heatmap",
"z": [[1,2],[3,4]],
"xaxis": "x2",
"yaxis": "y2",
"colorbar": {"y": 0.225, "len": 0.45}
}],
"layout": {
"yaxis":{"domain": [0, 0.45], "title": {"text": "$y=\\sin{2 \\theta}$"}},
"xaxis":{
"domain": [0, 0.45],
"title": {"text": "$x=\\int_0^a a^2+1$"},
"tickvals": [0, 1],
"ticktext": ["$\\frac{0}{100}$", "$\\frac{100}{100}$"]
},
"xaxis2": {"domain": [0.85, 1], "anchor": "y2"},
"yaxis2": {
"domain": [0, 0.45],
"anchor": "x2",
"title": {"text": "$(||01\\rangle+|10\\rangle)/\\sqrt2$"}
},
"height":500,
"width":800,
"margin": {"r": 250},
"title": {"text": "$i\\hbar\\frac{d\\Psi}{dt}=-[V-\\frac{-\\hbar^2}{2m}\\nabla^2]\\Psi$"},
"annotations":[
{
"text":"$(top,left)$","showarrow":false,"xref":"paper","yref":"paper",
"xanchor":"left","yanchor":"top","x":0,"y":1,"textangle":10,
"bordercolor":"#0c0","borderpad":3,"bgcolor":"#dfd"
},
{
"text":"$(right,bottom)$","xref":"paper","yref":"paper",
"xanchor":"right","yanchor":"bottom","x":0.2,"y":0.7,"ax":-20,"ay":-20,
"textangle":-30,"bordercolor":"#0c0","borderpad":3,"bgcolor":"#dfd",
"opacity":0.5
},
{"text":"$not-visible$", "visible": false},
{
"text":"$^{29}Si$","x":0.7,"y":0.7,"showarrow":false,
"xanchor":"right","yanchor":"top"
},
{
"text":"$^{17}O$","x":0.7,"y":0.7,"ax":15,"ay":-15,
"xanchor":"left","yanchor":"bottom"
}
]
}
}
app.layout=html.Div(children=[
dcc.Markdown('# h1 tag with inline MathJax: $E=mc^2$', mathjax=True),
dcc.Markdown(''' ## h2 tag with MathJax block: $$\\frac{1}{(\\sqrt{\\phi \\sqrt{5}}-\\phi) e^{\\frac25 \\pi}} = 1+\\frac{e^{-2\\pi}} {1+\\frac{e^{-4\\pi}} {1+\\frac{e^{-6\\pi}} {1+\\frac{e^{-8\\pi}} {1+\\ldots} } } } $$ ## Next line. ''', mathjax=True),
dcc.Graph(
mathjax=True,
id='graph-with-math',
figure=fig
),
dcc.Markdown('### No MathJax: Apple: $2, Orange: $3'),
dcc.Graph(
id='graph-without-math',
figure=fig
)
])
if__name__=='__main__':
app.run_server(debug=True)

Comment threadcomponents/dash-core-components/package.json Outdated
Comment threadcomponents/dash-core-components/package.json Outdated
Comment threadcomponents/dash-core-components/package.json
inlineMath: props => (
<Math tex={props.value} inline={true} />
),

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Thanks to @anders-kiaer for the contribution in 🏆 🏆 🏆 plotly/dash-core-components#634 🏆 🏆 🏆

Comment threadcomponents/dash-core-components/tests/integration/markdown/test_markdown.py Outdated
Comment threadcomponents/dash-core-components/tests/integration/markdown/test_markdown.py Outdated
Comment threadcomponents/dash-core-components/tests/integration/markdown/test_markdown.py Outdated
Comment threadcomponents/dash-core-components/tests/integration/graph/test_graph_varia.py Outdated
Comment threadcomponents/dash-core-components/tests/integration/graph/test_graph_varia.py Outdated
Comment threadcomponents/dash-core-components/src/fragments/Math.react.js Outdated
Comment threadcomponents/dash-core-components/src/fragments/Math.react.js Outdated
Comment threadcomponents/dash-core-components/src/fragments/Graph.react.js Outdated
Comment threadcomponents/dash-core-components/src/fragments/Graph.react.js Outdated
archmojand others added 2 commits March 1, 2022 23:07
…t_markdown.py
Co-authored-by: Alex Johnson <johnson.alex.c@gmail.com>
Co-authored-by: Alex Johnson <johnson.alex.c@gmail.com>
archmojand others added 2 commits March 10, 2022 09:40
…t_markdown.py
Co-authored-by: Alex Johnson <johnson.alex.c@gmail.com>
…t_markdown.py
Co-authored-by: Alex Johnson <johnson.alex.c@gmail.com>
Comment threadcomponents/dash-core-components/tests/integration/markdown/test_markdown.py Outdated
Comment threadcomponents/dash-core-components/src/utils/LazyLoader/mathjax.js Outdated
@archmoj

archmoj commented Mar 12, 2022

Copy link
Copy Markdown
ContributorAuthor

@alexcjohnson How does the percy image test for MathJax look like?

@alexcjohnson

Copy link
Copy Markdown
Collaborator

Looks good! Some parts of the graphs are grossly misaligned, but that's unfortunately normal for percy, we're missing some CSS from plotly.js
Screen Shot 2022-03-11 at 19 17 57

@alexcjohnsonalexcjohnson left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

💃 Fantastic work @archmoj!

@alexcjohnson
alexcjohnson merged commit f378199 into devMar 12, 2022
@alexcjohnson
alexcjohnson deleted the mathjax-v3 branch March 12, 2022 01:25
Sign up for freeto 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.

2 participants

@archmoj@alexcjohnson