Skip to content

Commit beb43b9

Browse files
jgd10margalvaJennaPaikowsky
authored
Examples from the cheat sheet (#283)
Three examples developed from code snippets presented in the cheat sheet that did not have equivalent examples in the docs. Co-authored-by: U-ANSYS\mgalvagn <[email protected]> Co-authored-by: JennaPaikowsky <[email protected]>
1 parent 2cf7b70 commit beb43b9

File tree

7 files changed

+304
-3
lines changed

7 files changed

+304
-3
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
.. _ref_launch_reporting_service:
3+
4+
Launching the Ansys Dynamic Reporting Service
5+
=============================================
6+
7+
To launch the service, provide an Ansys installation directory as a string.
8+
You can provide an existing, empty, directory if you intend to create a database.
9+
10+
.. note::
11+
This example assumes that you have a local Ansys installation.
12+
13+
"""
14+
15+
import ansys.dynamicreporting.core as adr
16+
17+
db_dir = "C:\\tmp\\my_local_db_directory"
18+
ansys_ins = "C:\\Program Files\\Ansys Inc\\v241"
19+
20+
adr_service = adr.Service(ansys_installation=ansys_ins, db_directory=db_dir)
21+
22+
23+
###############################################################################
24+
# Starting the Service
25+
# ~~~~~~~~~~~~~~~~~~~~
26+
# Once a `Service` object has been created, it must be started. It can be
27+
# similarly stopped.
28+
#
29+
30+
session_guid = adr_service.start(create_db=True)
31+
32+
# To stop the service
33+
adr_service.stop()
34+
35+
###############################################################################
36+
# Connecting to a remote Service
37+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
38+
# You may need to connect to a service that is already running. To do so create
39+
# a Service object, as before, but leave off the database argument and this time,
40+
# call the `connect` method and provide connection details, including any
41+
# credentials required. If no username and password were set when creating the
42+
# database, you can leave these fields empty and the default values will be
43+
# used.
44+
#
45+
46+
ansys_ins = r"C:\Program Files\Ansys Inc\v241"
47+
adr_service = adr.Service(ansys_installation=ansys_ins)
48+
adr_service.connect(url="http://localhost:8000", username="user", password="p455w0rd")
49+
50+
# To stop the service
51+
# sphinx_gallery_thumbnail_path = '_static/00_create_db_0.png'
52+
adr_service.stop()

doc/source/examples_source/00-basic/00-create_db.py renamed to doc/source/examples_source/00-basic/01-create_db.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
22
.. _ref_createdb:
33
4-
Create a database
5-
=================
4+
Create a database and populate it
5+
=================================
66
77
This example shows how to use PyDynamicReporting to create an Ansys
88
Dynamic Reporting service, create a database for this service, and

doc/source/examples_source/00-basic/01-connect.py renamed to doc/source/examples_source/00-basic/02-connect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
# ---------------------------------
5959
#
6060
# Now that you have a running Ansys Dynamic Reporting service, create a
61-
# second instance of the ``Reporting`` class and use it to
61+
# second instance of the ``Service`` class and use it to
6262
# connect to the database. Visualize the default report.
6363

6464
connected_s = adr.Service()
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""
2+
.. _ref_set_plot_properties:
3+
4+
How to set plot properties
5+
==========================
6+
7+
When working with a table, you can turn it into a plot by specifying
8+
the plot type through the `plot` property. Once a table is converted
9+
you can alter all sorts of plot properties by accessing properties on
10+
the item object.
11+
12+
.. note::
13+
This example assumes that you have a local Ansys installation.
14+
15+
Initially, you must create and start a session as per other examples.
16+
17+
"""
18+
19+
###############################################################################
20+
# Start an Ansys Dynamic Reporting service
21+
# ----------------------------------------
22+
#
23+
# Start an Ansys Dynamic Reporting service on a new
24+
# database. The path for the database directory must be to an empty directory.
25+
#
26+
27+
import numpy as np
28+
29+
import ansys.dynamicreporting.core as adr
30+
31+
db_dir = "C:\\tmp\\new_database"
32+
adr_service = adr.Service(db_directory=db_dir)
33+
session_guid = adr_service.start(create_db=True)
34+
35+
###############################################################################
36+
# Create a simple table
37+
# ---------------------
38+
#
39+
# Start by creating a simple table and visualizing it. Create a table
40+
# with 5 columns and 2 rows.
41+
#
42+
43+
my_table = adr_service.create_item(obj_name="Table")
44+
my_table.table_dict["rowlbls"] = ["Row 1", "Row 2"]
45+
my_table.item_table = np.array(
46+
[["1", "2", "3", "4", "5"], ["1", "4", "9", "16", "25"]], dtype="|S20"
47+
)
48+
49+
###############################################################################
50+
# Once you have created a table, set it to be a plot by changing
51+
# its properties, and then you can set other properties.
52+
#
53+
54+
# Set visualization to be plot instead of table
55+
my_table.plot = "line"
56+
57+
# Set X axis and axis formatting
58+
my_table.xaxis = "Row 1"
59+
my_table.format = "floatdot1"
60+
61+
###############################################################################
62+
# Properties can also be inspected this way.
63+
#
64+
65+
print(my_table.type)
66+
67+
# Close the service
68+
# -----------------
69+
#
70+
# Close the Ansys Dynamic Reporting service. The database with the items that
71+
# were created remains on disk.
72+
73+
# sphinx_gallery_thumbnail_path = '_static/00_create_db_0.png'
74+
adr_service.stop()
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""
2+
.. _ref_all_plot_properties:
3+
4+
Explore plot properties
5+
=======================
6+
7+
When working with a table, you can turn it into a
8+
plot by specifying the plot type through the `plot` property.
9+
This example demonstrates a variety of the possible plot
10+
properties available.
11+
12+
.. note::
13+
This example assumes that you have a local Ansys installation.
14+
15+
Initially, create and start a session as per other examples.
16+
17+
"""
18+
19+
###############################################################################
20+
# Start an Ansys Dynamic Reporting service
21+
# ----------------------------------------
22+
#
23+
# Start an Ansys Dynamic Reporting service on a new
24+
# database. The path for the database directory must be to an empty directory.
25+
#
26+
27+
import numpy as np
28+
29+
import ansys.dynamicreporting.core as adr
30+
31+
db_dir = "C:\\tmp\\new_database"
32+
adr_service = adr.Service(db_directory=db_dir)
33+
session_guid = adr_service.start(create_db=True)
34+
35+
###############################################################################
36+
# Create a simple table
37+
# ---------------------
38+
#
39+
# Start by creating a simple table and visualizing it. Create a table
40+
# with 5 columns and 2 rows.
41+
#
42+
43+
my_table = adr_service.create_item(obj_name="Table")
44+
my_table.table_dict["rowlbls"] = ["Row 1", "Row 2"]
45+
my_table.item_table = np.array(
46+
[["1", "2", "3", "4", "5"], ["1", "4", "9", "16", "25"]], dtype="|S20"
47+
)
48+
49+
###############################################################################
50+
# Once you have created a table, set it to be a plot by changing
51+
# its properties, and then you can set other properties.
52+
#
53+
54+
# Set visualization to be plot instead of table
55+
my_table.plot = "line"
56+
57+
# Set X axis and axis formatting
58+
my_table.xaxis = "Row 1"
59+
my_table.format = "floatdot1"
60+
61+
###############################################################################
62+
# Some rules on properties
63+
# ------------------------
64+
# - If a property is not relevant to a plot and it is changed, nothing will happen
65+
# - Plots are not dynamically updated. Subsequent `visualize` calls are needed
66+
# - Plots can have `visualize()` called repeatedly without exhausting the object
67+
#
68+
69+
my_table.line_color = "black"
70+
# This won't appear on our 2D plot or affect its output
71+
my_table.zaxis = "z-axis"
72+
my_table.visualize()
73+
74+
# Sets the x-axis limits and similar patterns work for yrange and zrange.
75+
my_table.xrange = [0, 3]
76+
my_table.visualize()
77+
78+
###############################################################################
79+
# Key properties
80+
# --------------
81+
# A few key properties are listed below as well as what they do, to get you started.
82+
#
83+
# - `xtitle`, `ytitle`, `ztitle`, `palette_title` - set the axis, and colorbar, labels
84+
# - `xrange`, `yrange, `zrange`, `palette_range` - set the axes amd colorbar limits
85+
# - `plot_title` - set the plot title
86+
# - `line_marker` - set the marker of scatter data, defaults to `circle`.
87+
# - `line_error_bars` - set y-axis error bars. Other axes are not available.
88+
# - `width`, `height` - dimensions of chart in pixels
89+
#
90+
91+
# Close the service
92+
# -----------------
93+
#
94+
# Close the Ansys Dynamic Reporting service. The database with the items that
95+
# were created remains on disk.
96+
97+
# sphinx_gallery_thumbnail_path = '_static/00_create_db_0.png'
98+
adr_service.stop()
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""
2+
.. _ref_create_report_templates:
3+
4+
Creating report templates
5+
=========================
6+
7+
Templates are used to specify how the final report will be organised. They
8+
can be nested to describe the layout of subsections in greater detail.
9+
10+
.. note::
11+
This example assumes that you have a local Ansys installation.
12+
13+
Initially, create and start a session as per other examples.
14+
15+
"""
16+
17+
###############################################################################
18+
# Start an Ansys Dynamic Reporting service
19+
# ----------------------------------------
20+
#
21+
# Start an Ansys Dynamic Reporting service on a new
22+
# database. The path for the database directory must be to an empty directory.
23+
# Get the serverobj property from the service. This property will be used to create the
24+
# template.
25+
#
26+
27+
import ansys.dynamicreporting.core as adr
28+
29+
db_dir = "C:\\tmp\\my_local_db_directory"
30+
ansys_ins = "C:\\Program Files\\Ansys Inc\\v241"
31+
adr_service = adr.Service(ansys_installation=ansys_ins, db_directory=db_dir)
32+
session_guid = adr_service.start(create_db=True)
33+
server = adr_service.serverobj
34+
35+
36+
###############################################################################
37+
# Create a template
38+
# ~~~~~~~~~~~~~~~~~~
39+
# The template is a plan of how ADR items will be presented in the final report.
40+
# The contents of sections is specified by filters that query the tags of items
41+
# in the database.
42+
#
43+
44+
45+
template_0 = server.create_template(name="My Report", parent=None, report_type="Layout:basic")
46+
47+
server.put_objects(template_0)
48+
49+
###############################################################################
50+
# Nesting templates
51+
# ~~~~~~~~~~~~~~~~~
52+
# Templates can be nested to describe layouts within a section, with the topmost template
53+
# being the report itself.
54+
#
55+
# Filters are composed of strings in a common format. The format is explained in more detail
56+
# on this page [Query Expressions](https://ansyshelp.ansys.com/public/account/secured?returnurl=/Views/Secured/corp/v251/en/adr_ug/adr_ug_query_expressions.html?q=query%20expression).
57+
#
58+
59+
template_1 = server.create_template(name="Intro", parent=template_0, report_type="Layout:panel")
60+
template_1.set_filter("A|i_type|cont|html,string;")
61+
62+
server.put_objects(template_1)
63+
server.put_objects(template_0)
64+
template_2 = server.create_template(name="Plot", parent=template_0, report_type="Layout:panel")
65+
template_2.set_filter("A|i_type|cont|table;")
66+
67+
server.put_objects(template_2)
68+
server.put_objects(template_0)
69+
70+
# Close the service
71+
# -----------------
72+
#
73+
# Close the Ansys Dynamic Reporting service. The database with the items that
74+
# were created remains on disk.
75+
76+
# sphinx_gallery_thumbnail_path = '_static/00_create_db_0.png'
77+
adr_service.stop()

0 commit comments

Comments
 (0)