-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path01-create_db.py
executable file
·130 lines (105 loc) · 4.23 KB
/
01-create_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""
.. _ref_createdb:
Create a database and populate it
=================================
This example shows how to use PyDynamicReporting to create an Ansys
Dynamic Reporting service, create a database for this service, and
create items in this database.
.. note::
This example assumes that you have a local Ansys installation.
"""
###############################################################################
# Start an Ansys Dynamic Reporting service
# ----------------------------------------
# Start an Ansys Dynamic Reporting service with a new database. The path for the
# database directory must be to an empty directory.
import numpy as np
import ansys.dynamicreporting.core as adr
import ansys.dynamicreporting.core.examples as examples
ansys_loc = r"C:\Program Files\ANSYS Inc\v232"
db_dir = r"C:\tmp\new_database"
adr_service = adr.Service(ansys_installation=ansys_loc, db_directory=db_dir)
session_guid = adr_service.start(create_db=True)
###############################################################################
# Create items
# ------------
#
# Now that an Ansys Dynamic Reporting service is running on top of the new
# database, create some items of different types (text, image, and 3D scene)
# in the database. The payload for these items comes from files on disk.
my_text = adr_service.create_item()
my_text.item_text = "<h1>Analysis Title</h1>This is the first of many items"
my_image = adr_service.create_item()
my_image.item_image = examples.download_file("enthalpy_001.png", "input_data")
my_scene = adr_service.create_item()
my_scene.item_scene = examples.download_file("dam_break.avz", "input_data")
###############################################################################
# Visualize all items
# -------------------
#
# Visualize all items currently in the database by invoking the
# default report, which is simply the list of items, one after the other.
# If you are running inside a Python interpreter such as Jupyter Notebook, the
# visualization is embedded in the web page. If not, a browser opens an
# HTML page that displays the default report.
adr_service.visualize_report()
###############################################################################
#
# .. image:: /_static/00_create_db_0.png
#
# Create tables and trees
# -----------------------
#
# Table and plot items can be generated by passing a numpy array. Trees
# are represented via Python dictionaries. All the different options for tables
# and trees can be set here.
my_plot = adr_service.create_item()
my_plot.item_table = np.array([[1, 2, 3, 4, 5, 6], [1, 4, 9, 16, 25, 36]], dtype="|S20")
my_plot.labels_row = ["First Row", "My Second Row"]
leaves = []
for i in range(5):
leaves.append({"key": "leaves", "name": f"Leaf {i}", "value": i})
children = []
children.append({"key": "child", "name": "Boolean example", "value": True})
children.append({"key": "child", "name": "Integer example", "value": 10})
children.append(
{
"key": "child_parent",
"name": "A child parent",
"value": "Parents can have values",
"children": leaves,
"state": "collapsed",
}
)
children.append({"key": "child", "name": "Float example", "value": 99.99})
tree = []
tree.append(
{"key": "root", "name": "Top Level", "value": None, "children": children, "state": "expanded"}
)
my_tree = adr_service.create_item(obj_name="My Tree object")
my_tree.item_tree = tree
###############################################################################
# Visualize tables and trees
# --------------------------
#
# You can visualize single items as web components, similarly to how you
# visualized the default report. Simply calling the
# :func:`visualize<ansys.dynamicreporting.core.Item.visualize>` method
# on each of the single items.
my_plot.visualize()
###############################################################################
#
# .. image:: /_static/00_create_db_1.png
#
my_tree.visualize()
###############################################################################
#
# .. image:: /_static/00_create_db_2.png
#
# Close the service
# -----------------
#
# Close the Ansys Dynamic Reporting service. The database with the items that
# were created remains on disk.
# sphinx_gallery_thumbnail_path = '_static/00_create_db_0.png'
adr_service.stop()