Displaying Image in RTF-Textfield using Pybis

Dear all,
I am currently using Pybis to upload and analyze my data. I like to display for example an diagram from the analysis in a text field of the experiment automatically via the python script. I had some success using this code in a rtf:

obj.p[“result”] = str(‘’)

I got the (temporary) url for the image from the dataset: dataset.file_links.values()
This approach works until the (download/user) session is expired. A permanent (external) url works fine.

Is there another way to permanently display images from the dataset using Pybis?

I’ve had a similar problem and tried to solve it with a dataset. I created a dataset with the image and linked the result in a word processor multiline varchar. This works, but the picture is only displayed if you “load” the image by hovering over it in the dataset to see a preview.

Thus, I asked for support for a better solution. I received the following answer:

Thank you for your patience. The issue you mention is possible to achieve with Pybis, although with a little trick. What you need to do is:

  1. Upload your image to a file service of the Openbis
  2. Attach uploaded file to a property

I’m adding an example code on how to do it in python:

import requests 
import json 

# Assuming you have logged into openbis via pybis

exp = openbis_instance.get_experiment('/DEFAULT/DEFAULT/DEFAULT_EXP_1') # Experiment or sample you want to update

file = '/path/to/my/image.png'
#
url = '{url_to_openbis_instance}/openbis/openbis/file-service/eln-lims?type=Files&sessionID=' + openbis_instance.token
files = {'file': open(file, 'rb')}
r = requests.post(url, files=files, verify=False) # verify=False is required if you use self-signed certificates
if r.ok:
    url = json.loads(r.text)['url'] # url is a special url to uploaded image
    formatted = f'<?xml version="1.0" encoding="UTF-8"?>\n<html><head></head><body><p>\xa0</p><figure class="image"><img src="{url}" /></figure></body></html>' # you need to anchor your image in the html to be visible in the property
    exp.props['$document'] = formatted # setting text with image to a example $document property 
    exp.save()

I hope this suits you as well! If you have any further questions, I can show you my code.

Dear Cedric,
thank you for the fast reply - your code worked perfectly.
Thank you, Mathias