Created
September 29, 2019 19:07
-
-
Save bonfus/fb9d118aee5eb7e46ff35060480eb670 to your computer and use it in GitHub Desktop.
Convert Magnetic Susceptibility with IPy widgets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# A very simple magnetic susceptibility conversion tool\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "20b8e93ff2c143b4b7f866f7718ffd12", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"VBox(children=(HBox(children=(Label(value='X in'), Text(value='', layout=Layout(width='190px'), placeholder='0…" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"from __future__ import division\n", | |
"import ipywidgets as ipw\n", | |
"\n", | |
"def mk_unit():\n", | |
" return ipw.Dropdown(\n", | |
" options=[('Xv SI [1]', 1),\n", | |
" ('Xv CGS [1 or emu]', 2),\n", | |
" ('Xm SI [m^3/kg]', 3), \n", | |
" ('Xm CGS [cm^3/g]', 4),\n", | |
" ('Xmol SI [m^3/mol]', 5),\n", | |
" ('Xmol CGS [cm^3/mol]', 6)\n", | |
" ],\n", | |
" value=1,\n", | |
" description='Units:',\n", | |
" )\n", | |
"\n", | |
"chiinput = (ipw.Text(placeholder=\"0\", layout=ipw.Layout(width=\"190px\"), disabled=False), mk_unit())\n", | |
"chioutput = (ipw.Text(placeholder=\"0\", layout=ipw.Layout(width=\"190px\"), disabled=True), mk_unit())\n", | |
"molar_mass_wdg = ipw.Text(placeholder=\"0\", layout=ipw.Layout(width=\"190px\"))\n", | |
"density_wdg = ipw.Text(placeholder=\"0\", layout=ipw.Layout(width=\"190px\"))\n", | |
"\n", | |
"def on_click(btn):\n", | |
" fourpi=12.566370614359172\n", | |
" input_to_SI_adim = 9.0E9 # A crazy number that should never be used.\n", | |
" try:\n", | |
" inv = float(chiinput[0].value)\n", | |
" except:\n", | |
" chioutput[0].value = \"Error: input value missing\"\n", | |
" return\n", | |
" try:\n", | |
" density = float(density_wdg.value)\n", | |
" except:\n", | |
" if chiinput[1].value >= 3 or chiinput[1].value >= 3:\n", | |
" chioutput[0].value = \"Error: input value missing\"\n", | |
" return\n", | |
" try:\n", | |
" molar_mass = float(molar_mass_wdg.value)\n", | |
" except:\n", | |
" if chiinput[1].value >= 5 or chiinput[1].value >= 5:\n", | |
" chioutput[0].value = \"Error: input value missing\"\n", | |
" return\n", | |
" \n", | |
" if chiinput[1].value == 1: # volume SI\n", | |
" input_to_SI_adim = inv\n", | |
" if chiinput[1].value == 2: # volume CGS\n", | |
" input_to_SI_adim = inv*fourpi\n", | |
" if chiinput[1].value == 3: # mass SI\n", | |
" input_to_SI_adim = inv * density * 1.0E3\n", | |
" if chiinput[1].value == 4: # mass CGS\n", | |
" input_to_SI_adim = inv * density * fourpi\n", | |
" if chiinput[1].value == 5: # mol SI\n", | |
" input_to_SI_adim = inv * (density/molar_mass) * 1.0E6\n", | |
" if chiinput[1].value == 6: # mol SI\n", | |
" input_to_SI_adim = inv * (density/molar_mass) * fourpi\n", | |
"\n", | |
" if chioutput[1].value == 1: # volume SI\n", | |
" outv = input_to_SI_adim\n", | |
" if chioutput[1].value == 2: # volume CGS\n", | |
" outv = input_to_SI_adim / fourpi\n", | |
" if chioutput[1].value == 3: # mass SI\n", | |
" outv = input_to_SI_adim / ( density * 1.0E3 )\n", | |
" if chioutput[1].value == 4: # mass CGS\n", | |
" outv = input_to_SI_adim / ( density * fourpi )\n", | |
" if chioutput[1].value == 5: # mol SI\n", | |
" outv = input_to_SI_adim /( (density/molar_mass) * 1.0E6)\n", | |
" if chioutput[1].value == 6: # mol SI\n", | |
" outv = input_to_SI_adim /( (density/molar_mass) * fourpi)\n", | |
" \n", | |
" chioutput[0].value = str(outv)\n", | |
" \n", | |
" \n", | |
"def mk_btn(description):\n", | |
" btn = ipw.Button(description=description, layout=ipw.Layout(width=\"245px\"))\n", | |
" btn.on_click(on_click)\n", | |
" return btn\n", | |
"\n", | |
"\n", | |
"row0 = ipw.HBox((ipw.Label('X in'), ) + chiinput)\n", | |
"row1 = ipw.HBox((ipw.Label('X out'),) + chioutput)\n", | |
"row2 = ipw.HBox((ipw.Label('Molar mass (g/mol)'), molar_mass_wdg, ipw.Label('Density (g/cm^3)'), density_wdg ))\n", | |
"ipw.VBox((row0, row1, row2, mk_btn('Convert')))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment