Skip to content

Commit f041769

Browse files
authored
Merge pull request #70 from ccdc-opensource/training_exercise
build a simple google translate example around simple_report.py (NO_J…
2 parents 51ff6c8 + e9392d6 commit f041769

File tree

1 file changed

+256
-0
lines changed

1 file changed

+256
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"```\n",
8+
"This script can be used for any purpose without limitation subject to the\n",
9+
"conditions at http://www.ccdc.cam.ac.uk/Community/Pages/Licences/v2.aspx\n",
10+
"\n",
11+
"This permission notice and the following statement of attribution must be\n",
12+
"included in all copies or substantial portions of this script.\n",
13+
"\n",
14+
"2024-09-11: Made available by the Cambridge Crystallographic Data Centre.\n",
15+
"\n",
16+
"```"
17+
]
18+
},
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {},
22+
"source": [
23+
"# International Report\n",
24+
"\n",
25+
"This notebook talks through how to create a report that reports a CSD entry ... but uses google translate to translate headings into the language of user choice.\n",
26+
"You could use the ideas her to convert the 'simple_report.py script into an international report\n",
27+
"\n",
28+
"#### Prerequisites\n",
29+
"\n",
30+
"First install the CSD Python API and googletrans into your conda or pip environment. This should only be needed the before you run the script."
31+
]
32+
},
33+
{
34+
"cell_type": "markdown",
35+
"metadata": {},
36+
"source": [
37+
"Now we are ready to start some python coding. We need to import some modules. We are writing a script that ultimately will be used in Mercury so we will want some of \n",
38+
"the special utilities available for writing reports."
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": null,
44+
"metadata": {},
45+
"outputs": [],
46+
"source": [
47+
"from ccdc.utilities import html_table\n"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"We can create a translator for the script to use, and define our language of choice. Lets write a little function to do the translation"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": null,
60+
"metadata": {},
61+
"outputs": [],
62+
"source": [
63+
"from googletrans import Translator\n",
64+
"\n",
65+
"translator = Translator()\n",
66+
"def tr(text, lang):\n",
67+
" if lang is None:\n",
68+
" return text\n",
69+
" try:\n",
70+
" return translator.translate(str(text),src=\"en\",dest=lang).text\n",
71+
" except Exception as e:\n",
72+
" return text"
73+
]
74+
},
75+
{
76+
"cell_type": "markdown",
77+
"metadata": {},
78+
"source": [
79+
"Let's try it out:"
80+
]
81+
},
82+
{
83+
"cell_type": "code",
84+
"execution_count": null,
85+
"metadata": {},
86+
"outputs": [],
87+
"source": [
88+
"\n",
89+
"tr(\"Mary had a little lamb\", \"ar\") # Lets try arabic!"
90+
]
91+
},
92+
{
93+
"cell_type": "markdown",
94+
"metadata": {},
95+
"source": [
96+
"So now we can create a report on a CSD entry that is international. The following function will work in a Mercury API script. Note how it defines an interface object"
97+
]
98+
},
99+
{
100+
"cell_type": "code",
101+
"execution_count": null,
102+
"metadata": {},
103+
"outputs": [],
104+
"source": [
105+
"def get_coordinates(molecule, round_digits=None):\n",
106+
" \"\"\"Yield the label and fractional coordinates of all atoms in the molecule.\n",
107+
"\n",
108+
" :param molecule: (:obj:`ccdc.molecule.Molecule`) The molecule for which to return coordinates.\n",
109+
" :param round_digits: (:obj:`int`) How many decimal digits coordinates should be rounded to.\n",
110+
" :returns: (:obj:`list`) List of the label and fractional x/y/z coordinates for each atom\n",
111+
" in the molecule in format ['Atom label', 'X coordinate', 'Y coordinate', 'Z coordinate'].\n",
112+
" \"\"\"\n",
113+
" for atom in molecule.atoms:\n",
114+
" try:\n",
115+
" x, y, z = atom.fractional_coordinates\n",
116+
" yield [atom.label,\n",
117+
" x if round_digits is None else round(x, round_digits),\n",
118+
" y if round_digits is None else round(y, round_digits),\n",
119+
" z if round_digits is None else round(z, round_digits)]\n",
120+
" except TypeError:\n",
121+
" continue\n",
122+
"\n",
123+
"\n",
124+
"def main(interface=None, lang=None):\n",
125+
" \"\"\"Generate a simple report based on the entry currently selected in the Mercury UI.\n",
126+
"\n",
127+
" :param interface: (:obj:`ccdc.utility.ApplicationInterface`) An ApplicationInterface instance.\n",
128+
" \"\"\"\n",
129+
" if interface is None:\n",
130+
" from ccdc.utilities import ApplicationInterface\n",
131+
" interface = ApplicationInterface()\n",
132+
"\n",
133+
" entry = interface.current_entry\n",
134+
"\n",
135+
" # Open a HTML report. This will create the file, copy the CSD Python API\n",
136+
" # default template for reports and fill in the headline/page title.\n",
137+
" with interface.html_report(title=tr('Simple report on ',lang)+ ' ' + entry.identifier) as report:\n",
138+
"\n",
139+
" # Write the section header for Entry Details\n",
140+
" report.write_section_header(tr('Entry Details',lang))\n",
141+
" # Assemble a list of information and labels to go into the \"Entry Details\" table\n",
142+
" entry_details = [\n",
143+
" ['<b>'+tr('Chemical name',lang)+'</b>', entry.chemical_name],\n",
144+
" ['<b>'+tr('Synonyms',lang)+'</b>', entry.synonyms],\n",
145+
" ['<b>'+tr('Formula',lang)+'</b>', entry.formula],\n",
146+
" ['<b>'+tr('R-factor',lang)+'</b>', entry.r_factor],\n",
147+
" ['<b>'+tr('Disorder',lang)+'</b>', tr(entry.disorder_details,lang)],\n",
148+
" ['<b>'+tr('Polymorphism',lang)+'</b>', tr(entry.polymorph,lang)],\n",
149+
" ['<b>'+tr('3D structure',lang)+'</b>', tr(entry.has_3d_structure,lang)],\n",
150+
" ['<b>'+tr('Organic',lang)+'</b>', tr(entry.is_organic,lang)],\n",
151+
" ['<b>'+tr('Polymeric',lang)+'</b>', tr(entry.is_polymeric,lang)],\n",
152+
" ['<b>'+tr('Bioactivity',lang)+'</b>', tr(entry.bioactivity,lang)],\n",
153+
" ['<b>'+tr('Source',lang)+'</b>', tr(entry.source,lang)],\n",
154+
" ['<b>'+tr('Habit',lang)+'</b>', tr(entry.habit,lang)],\n",
155+
" ]\n",
156+
" # Generate a HTML table from the entry details and write it to the report\n",
157+
" report.write(html_table(data=entry_details, table_id='entry_details'))\n",
158+
"\n",
159+
" # Write the section header for Fractional Coordinates\n",
160+
" report.write_section_header(tr('Fractional Coordinates',lang))\n",
161+
" # Get the coordinates of all the atoms from the entry and write them to a HTML table\n",
162+
" report.write(html_table(data=list(get_coordinates(entry.molecule, round_digits=3)),\n",
163+
" table_id='fractional_coordinates',\n",
164+
" header=[tr('Atom',lang), 'x', 'y', 'z']))\n",
165+
"\n",
166+
" # Write the section header for Publication Details\n",
167+
" report.write_section_header(tr('Publication Details',lang))\n",
168+
" # Assemble a list of information and labels for the Publication Details table\n",
169+
" publication_details = [\n",
170+
" ['<b>'+tr('Reference',lang)+'</b>', '%s Volume %s, %s' % (getattr(entry.publication, 'journal_name', ''),\n",
171+
" entry.publication.volume,\n",
172+
" entry.publication.year)],\n",
173+
" ['<b>'+tr('Authors', lang)+'</b>', entry.publication.authors],\n",
174+
" ['<b>'+tr('Document Object Identifier',lang)+'</b>', entry.publication.doi]\n",
175+
" ]\n",
176+
" # Write the publication details to a HTML table\n",
177+
" report.write(html_table(publication_details, table_id='publication_details'))\n",
178+
"\n",
179+
" # Write the section header for Basic Crystallographic Information\n",
180+
" report.write_section_header(tr('Basic Crystallographic Information',lang))\n",
181+
" # Assemble a list of basic crystal information and labels for the table\n",
182+
" crystallographic_data = [\n",
183+
" ['<b>'+tr('Crystal System',lang)+'</b>', entry.crystal.crystal_system],\n",
184+
" ['<b>'+tr('Space Group',lang) + '</b>', entry.crystal.spacegroup_symbol],\n",
185+
" ['<b>'+tr('Cell Volume',lang)+ '</b>', '%s ų' % round(entry.crystal.cell_volume, 3)],\n",
186+
" ['<b>Z, Z\\'</b>', entry.crystal.z_prime],\n",
187+
" ]\n",
188+
" # Write the crystallographic details to a HTML table\n",
189+
" report.write(html_table(crystallographic_data, table_id='crystallographic_information'))\n",
190+
"\n",
191+
" # Once the HTMLReport is closed (e.g. when the with: branch above ends),\n",
192+
" # it will automatically write the appropriate HTML footer."
193+
]
194+
},
195+
{
196+
"cell_type": "markdown",
197+
"metadata": {},
198+
"source": [
199+
"Lets use our function. As we are in a notebook, we will have to define a dummy interface file:"
200+
]
201+
},
202+
{
203+
"cell_type": "code",
204+
"execution_count": null,
205+
"metadata": {},
206+
"outputs": [],
207+
"source": [
208+
"import time\n",
209+
"from ccdc.utilities import ApplicationInterface\n",
210+
"interface = ApplicationInterface(parse_commandline=False)\n",
211+
"interface.identifier = \"AABHTZ\"\n",
212+
"interface.output_html_file = f'{interface.identifier}_{time.strftime(\"%H%M%S\", time.gmtime())}_report.html'\n",
213+
"\n",
214+
"main(interface,\"ar\") # Arabic "
215+
]
216+
},
217+
{
218+
"cell_type": "markdown",
219+
"metadata": {},
220+
"source": [
221+
"Finally use Jupyter to display it:"
222+
]
223+
},
224+
{
225+
"cell_type": "code",
226+
"execution_count": null,
227+
"metadata": {},
228+
"outputs": [],
229+
"source": [
230+
"from IPython.display import HTML\n",
231+
"HTML(interface.output_html_file)"
232+
]
233+
}
234+
],
235+
"metadata": {
236+
"kernelspec": {
237+
"display_name": "Python 3 (ipykernel)",
238+
"language": "python",
239+
"name": "python3"
240+
},
241+
"language_info": {
242+
"codemirror_mode": {
243+
"name": "ipython",
244+
"version": 3
245+
},
246+
"file_extension": ".py",
247+
"mimetype": "text/x-python",
248+
"name": "python",
249+
"nbconvert_exporter": "python",
250+
"pygments_lexer": "ipython3",
251+
"version": "3.9.20"
252+
}
253+
},
254+
"nbformat": 4,
255+
"nbformat_minor": 4
256+
}

0 commit comments

Comments
 (0)