> ## Documentation Index
> Fetch the complete documentation index at: https://developer.synopsys.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Add Files, Pictures, and Hyperlinks

Add File, Picture, and Hyperlink attribute types to MI.

## Connect to MI

Specify a database and table.

```python theme={null}
from datetime import datetime
import pathlib
from GRANTA_MIScriptingToolkit import granta as mpy

mi = mpy.connect('http://my.server.name/mi_servicelayer', autologon=True)
db = mi.get_db(db_key='MI_Training')
tab = db.get_table('Tensile Test Data')
```

## Create a new record

Define a path in the table from a starting folder (in this case the top level folder) using `path_from()`.
If the path does not exist, the required folders will be created.
Specify `end_node` to create a new **Record** object at the end of the path with that name.

```python theme={null}
now = datetime.now().strftime("%c")
record_name = 'STK Example 11:{}'.format(now)
record = tab.path_from(None, tree_path=['High Alloy Steels', 'AMS 6520', 'Plate', '300°F'], end_node=record_name)
record
```

*Previous cell output:*

```output theme={null}
<Record long name: STK Example 11:Wed Oct 30 16:07:16 2024>
```

Access the (empty) **AttributeFile**, **AttributePicture**, and **AttributeHyperlink** objects.

```python theme={null}
new_file = record.attributes['Test File 1']
new_pict = record.attributes['Image 2']
test_method_link = record.attributes['Standard Tension Testing Method']
test_method_link
```

*Previous cell output:*

```output theme={null}
<HyperlinkValue name: Standard Tension Testing Method, url: None>
```

## Add files to the record

To add a file to a file attribute (or a picture to a picture attribute) there are two approaches.

Create a **File** or **Picture** object, and set the `binary_data` property directly with either a file buffer or
bytes object. (Useful when you have a file already loaded in your script.)

```python theme={null}
file_object = mpy.File()
file_path = pathlib.Path('supporting_files/11_example_file_for_import.txt')
with open(file_path, 'rb') as file_buffer:
    file_object.binary_data = file_buffer.read()
file_object.file_name = file_path.name
file_object.description = 'This is an example file'
new_file.object = file_object
new_file
```

*Previous cell output:*

```output theme={null}
<FileValue name: Test File 1, file_name: 11_example_file_for_import.txt, description: This is an exampl...>
```

Use the **AttributePicture.load()** method to load a file from disk with a provided file name. (Useful when you have
a file saved to disk).

```python theme={null}
picture = pathlib.Path('supporting_files/11_example_image_for_import.jpg')
new_pict.load(picture)
new_pict
```

*Previous cell output:*

```output theme={null}
<PictureValue name: Image 2, populated: True>
```

## Add hyperlinks to the record

To add a hyperlink to a hyperlink attribute, set the `value` property. Optionally, you can also set the `hyperlink_display`
and/or `hyperlink_description` properties.

```python theme={null}
test_method_link.value = 'https://www.astm.org/Standards/E8'
test_method_link.hyperlink_display = 'New'
test_method_link.hyperlink_description = 'Specification'
test_method_link
```

*Previous cell output:*

```output theme={null}
<HyperlinkValue name: Standard Tension Testing Method, url: https://www.astm.org/Standards/E8>
```

Set the new attributes to update, and write the changes to MI. Specifying `include_binary_data_in_refresh = True` means that
the picture data will be included in the resulting `Record` object, rather than a URL reference to the picture data.

```python theme={null}
record.set_attributes([new_file, new_pict, test_method_link])
record = mi.update([record], include_binary_data_in_refresh=True)[0]
```

## Save files attached to a record

Verify your changes by first extracting the new image from the record, then saving it to disk.
You can either access the `value` directly or provide a file path to the `save()` method.

First, ensure the output directory exists. This directory will already exist if a notebook
was run previously that saved a file to disk.

```python theme={null}
from pathlib import Path
output_folder = Path("./output")
output_folder.mkdir(exist_ok=True)
```

Next, save the picture to disk.

```python theme={null}
new_pict = record.attributes['Image 2']
from IPython.display import Image
Image(new_pict.value)
```

![jpeg](https://mintcdn.com/synopsys-00a47cbc/Srh1_f--TdKCLyqD/docs/granta-mi/granta-mi-scripting-toolkit/4-1/samples/streamlined/11_Add_Files_Pictures_and_Hyperlinks_files/11_Add_Files_Pictures_and_Hyperlinks_20_0.jpg?fit=max\&auto=format\&n=Srh1_f--TdKCLyqD\&q=85\&s=cccfb0522cb4b98f14f020e3ce92e032)

```python theme={null}
new_pict.save(path='output/11_exported_picture.jpg')
```
