.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/01_tips_n_tricks/example_01_run_python_script_output.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_01_tips_n_tricks_example_01_run_python_script_output.py: .. _ref_example_01_run_python_script_output: Output to different formats and handle an error ----------------------------------------------- This example calls the ``run_python_script`` method and gets the output in string, JSON, and CSV formats. It also handles an error scenario. .. GENERATED FROM PYTHON SOURCE LINES 12-17 Launch mechanical ~~~~~~~~~~~~~~~~~ Launch a new Mechanical session in batch, setting the ``cleanup_on_exit`` argument to ``False``. To close this Mechanical session when finished, this example must call the ``mechanical.exit()`` method. .. GENERATED FROM PYTHON SOURCE LINES 17-26 .. code-block:: Python import json from ansys.mechanical.core import launch_mechanical import grpc mechanical = launch_mechanical(batch=True, cleanup_on_exit=False) print(mechanical) .. rst-class:: sphx-glr-script-out .. code-block:: none Ansys Mechanical [Ansys Mechanical Enterprise] Product Version:242 Software build date: 06/03/2024 09:35:09 .. GENERATED FROM PYTHON SOURCE LINES 27-30 Get and output a simple string ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Run the script to get a simple string output. .. GENERATED FROM PYTHON SOURCE LINES 30-41 .. code-block:: Python output = mechanical.run_python_script( """ def return_string(): return "hello world" return_string() """ ) print(f"string output={output}") .. rst-class:: sphx-glr-script-out .. code-block:: none string output=hello world .. GENERATED FROM PYTHON SOURCE LINES 42-45 Output string as JSON ~~~~~~~~~~~~~~~~~~~~~ Run the script to get the string output as JSON. .. GENERATED FROM PYTHON SOURCE LINES 45-62 .. code-block:: Python output = mechanical.run_python_script( """ def return_json(): import json dict = {"value1": 100, "value2": 200} json_text = json.dumps(dict) return json_text return_json() """ ) print(f"json output={output}") my_dict = json.loads(output) print(f"Parsed json: value1={my_dict['value1']}, value2={my_dict['value2']}") .. rst-class:: sphx-glr-script-out .. code-block:: none json output={"value2": 200, "value1": 100} Parsed json: value1=100, value2=200 .. GENERATED FROM PYTHON SOURCE LINES 63-66 Output string as CSV ~~~~~~~~~~~~~~~~~~~~ Run the script to get the string output as CSV. .. GENERATED FROM PYTHON SOURCE LINES 66-79 .. code-block:: Python output = mechanical.run_python_script( """ def return_csv(): return "1,2,3" return_csv() """ ) print(f"csv output={output}") csv_values = output.split(sep=",") print(f"Parsed csv: {';'.join(csv_values)}") .. rst-class:: sphx-glr-script-out .. code-block:: none csv output=1,2,3 Parsed csv: 1;2;3 .. GENERATED FROM PYTHON SOURCE LINES 80-83 Handle an error scenario ~~~~~~~~~~~~~~~~~~~~~~~~ Run the script and handle the error. .. GENERATED FROM PYTHON SOURCE LINES 83-88 .. code-block:: Python try: output = mechanical.run_python_script("hello_world()") except grpc.RpcError as error: print(f"Error: {error.details()}") .. rst-class:: sphx-glr-script-out .. code-block:: none Error: name 'hello_world' is not defined .. GENERATED FROM PYTHON SOURCE LINES 89-92 Close mechanical ~~~~~~~~~~~~~~~~ Close the mechanical instance. .. GENERATED FROM PYTHON SOURCE LINES 92-94 .. code-block:: Python mechanical.exit() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.193 seconds) .. _sphx_glr_download_examples_01_tips_n_tricks_example_01_run_python_script_output.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_01_run_python_script_output.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_01_run_python_script_output.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_01_run_python_script_output.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_