Chemistry Overview¶
A wide range of tools exist that support workflows in Chemistry, from looking up the structure and properties of a wide variety of elements and compounds, to visualising their structure using interactive HTML widgets.
Using automation to help us generate the the lookup of compound structures from their names allows us to create narratives where correctness is guaranteed when moving from one consideration, such as the formula for a particular compound given its common name, to the visualisation of that structure, or to a consideration of its physical, structural or chemical properties.
Hiding Code
The following example includes the code inline to show how the automation proceeds. In a finished would the code could be hidden but revealable, for example in collapsed code cells, or have all mention of the code removed from the final output document.
Example - Describing a Compound¶
As an example of providing a generated description of a compound simply from its name, let’s consider ethanol. (We could just as easily have picked another compound, such as a methane or nitric acid.
Let’s define a reference to the compound:
#Provide the common name of a compound
compound_name = "ethanol"
At the current time, whilst R based bookdown
workflows do support inline embedding of code variables in markdown text, interactive Jupyter notebook markdown cells don’t support such a feature (although there is ongoing work to provide this sort of support) other than by extension.
However, it is possible to embed variables into markdown text in Jupyter Book workflows using jupyter-glue
.
from myst_nb import glue
# Create a reference to a value we can use in our markdown text
glue("compound", compound_name, display=False)
Having declared the compound we want to investigate in code, we can refer to it directly inline in our text using a {glue:}`compound`
reference: ethanol.
We can also automatically look-up various properties associated with the compound, such as its chemical formula or a universal compound identifier.
import pubchempy as pcp
_compound = pcp.get_compounds(compound_name, 'name')[0]
The formula can be rendered in an appropriate typographical form from a LaTeX representation of the formula.
from IPython.display import Latex
Latex('$\ce{'+_compound.molecular_formula+'}$')
It is also possible to create glue
references to things like the compound LaTeX equation.
Using the mhchem
MathJax package, we easily add support for inline rendering of chemical equations, just as we can rendered mathematical equations:
_compound_latex = '$\ce{'+_compound.molecular_formula+'}$'
# Save a reference to the Latex equivalent of the compound formula
glue("compoundLatex", Latex(_compound_latex), display=False)
This means that we can render the chemical equation for our chosen compound (ethanol) in a markdown content block:
We can also render a free standing HTML+JS 3D interactive version of the molecule into the page from the previously retrieved universal compound ID:
import py3Dmol
# Lookup a molecule using its CID (PubChem Compound Identification) code
p=py3Dmol.view(query = f'cid:{_compound.cid}')
# Set the render style
p.setStyle({'stick': {'radius': .1}, 'sphere': {'scale': 0.25}})
p.show()
You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
jupyter labextension install jupyterlab_3dmol
If we were to change the name of the compound in the first code cell in our original source document and reflow the notebook, everything would be updated and there would be no mismatches between the compound name, its formula, or its visualised structure.
This ability to write general templated content and then generate final versions of it that reference particular compounds allows easy updating of materials if you want to change the focus of discussion. It might be particularly appropriate when creating worksheets or reference sheets associated with a particular compound, for example. It also provides an easy way to generate templated assessment questions and marking guides that can be “refreshed” simply by aupdating a question to apply to a compound not previously the focus of the same assessment question.
Code visibility in rendered documents
Recall that there is no requirement for the original generating code to be visible, or even present, in the final rendered document.