# Python Code Snippets This page extends [Keyword Substitution](keyword_substitution.md) with Python blocks in template {term}`project support files `. The blocks calculate material and source values from {term}`project keys` before JCMsuite runs the project. ## Define Project Keys The project keys define the rod radius, the refractive indices, the vacuum wavelength, and the polarization angle: ```{literalinclude} ../../demos/documentation_tutorials/getting_started/code_snippets/run.py :language: python :lines: 6-13 ``` The radius is $r = 0.3\,\mathrm{\mu m}$. The refractive indices are $n_{\mathrm{air}} = 1.0$ and $n_{\mathrm{glass}} = 1.52$. The source has a vacuum wavelength of $\lambda_0 = 0.550\,\mathrm{\mu m}$ and a polarization angle of $\vartheta = 45^\circ$. The template calculates the incident field as: $$ \mathbf{E}_{\mathrm{illum}}(\mathbf{x}) = \begin{pmatrix} 0 \\ \sin(\vartheta) \\ \cos(\vartheta) \end{pmatrix} \exp\left(i\,(k_x, 0, 0)\,\mathbf{x}\right), $$ where $k_x = 2\pi n_{\mathrm{air}} / \lambda_0$. ## Use Python Blocks A Python block starts with ``. The template evaluates the block as Python code. The block can read and update project keys. `numpy` is imported by default. Text outside the block remains JCM input text. The `materials.jcmt` template calculates the relative permittivity of air and glass from the refractive indices. The template then substitutes the calculated values into the `Material` sections: ```{literalinclude} ../../demos/documentation_tutorials/getting_started/code_snippets/materials.jcmt :language: jcmt ``` The `sources.jcmt` template converts the vacuum wavelength to metres. The template calculates the wave vector and the polarization vector. The `Source` section then uses the calculated project keys: ```{literalinclude} ../../demos/documentation_tutorials/getting_started/code_snippets/sources.jcmt :language: jcmt ``` ## Run The Project ```{literalinclude} ../../demos/documentation_tutorials/getting_started/code_snippets/run.py :language: python ``` {func}`solve ` renders each template project support file and returns a {class}`SolveResult `. The script exports the first postprocess data with {meth}`to_dict() ` and prints the exterior energy flux. The script selects the first exported field from the second postprocess and displays its intensity. ## Project Files
run.py ```{literalinclude} ../../demos/documentation_tutorials/getting_started/code_snippets/run.py :language: python ```
layout.jcmt ```{literalinclude} ../../demos/documentation_tutorials/getting_started/code_snippets/layout.jcmt :language: jcmt ```
materials.jcmt ```{literalinclude} ../../demos/documentation_tutorials/getting_started/code_snippets/materials.jcmt :language: jcmt ```
mie2D.jcmp ```{literalinclude} ../../demos/documentation_tutorials/getting_started/code_snippets/mie2D.jcmp :language: jcm ```
sources.jcmt ```{literalinclude} ../../demos/documentation_tutorials/getting_started/code_snippets/sources.jcmt :language: jcmt ```
## Next Step Continue to [Loops](loops.md) to generate repeated JCM input text from a Python block.