chemistry_tools.formulae.html

Attention

This module has the following additional requirements:

cawdrey>=0.5.0
mathematical>=0.5.1
pyparsing>=2.4.6
tabulate>=0.8.9

These can be installed as follows:

python -m pip install chemistry-tools[formulae]

Functions and constants for converting formulae to HTML.

Functions:

html_subscript(val)

Returns the HTML subscript of the given value.

html_superscript(val)

Returns the HTML superscript of the given value.

string_to_html(formula[, prefixes, infixes, …])

Convert formula string to HTML string representation.

html_subscript(val)[source]

Returns the HTML subscript of the given value.

Parameters

val (Union[str, float]) – The value to superscript

Return type

str

html_superscript(val)[source]

Returns the HTML superscript of the given value.

Parameters

val (Union[str, float]) – The value to subscript

Return type

str

string_to_html(formula, prefixes=None, infixes=None, suffixes=('(s)', '(l)', '(g)', '(aq)'))[source]

Convert formula string to HTML string representation.

Examples:

>>> string_to_html("NH4+")
'NH<sub>4</sub><sup>+</sup>'
>>> string_to_html("Fe(CN)6+2")
'Fe(CN)<sub>6</sub><sup>2+</sup>'
>>> string_to_html("Fe(CN)6+2(aq)")
'Fe(CN)<sub>6</sub><sup>2+</sup>(aq)'
>>> string_to_html(".NHO-(aq)")
'&sdot;NHO<sup>-</sup>(aq)'
>>> string_to_html("alpha-FeOOH(s)")
'&alpha;-FeOOH(s)'
Parameters
  • formula (str) – Chemical formula, e.g. 'H2O', 'Fe+3', 'Cl-'

  • prefixes (Optional[Dict[str, str]]) – Mapping of prefixes to their HTML equivalents. Default greek letters and .

  • infixes (Optional[Dict[str, str]]) – Mapping of infixes to their HTML equivalents. Default None.

  • suffixes (Sequence[str]) – Suffixes to keep. Default ('(s)', '(l)', '(g)', '(aq)').

Return type

str

Returns

The HTML representation of the formula