Skip to content

Instantly share code, notes, and snippets.

@MrCreosote
Last active February 12, 2024 19:19
Show Gist options
  • Save MrCreosote/d4d2c5d41f517ad3fd7d9a5634fb9612 to your computer and use it in GitHub Desktop.
Save MrCreosote/d4d2c5d41f517ad3fd7d9a5634fb9612 to your computer and use it in GitHub Desktop.
Hackily add images to a Narrative and store said images in KBase
  • Note: this is not a reasonable general solution for images in narratives because:
    • Even if all the steps below were automated for the user, it suffers from the so called "copy of a copy" problem. Namely, if the narrative is copied by a different user and the user loses access to the source narrative, the narrative will no longer be able to access the report object embedded in the HtmlFileSetService URL.
    • The HTMLFileSetService dynamic url is hardcoded into the narrative markdown text and it's, well, dynamic. It can change at any time at which point the images will 404.

Procedure:

  • Add all your images to a zip file
  • Using your kbase token, upload the zip file to Shock
$ curl -H "Authorization: OAuth $KBASE_TOKEN_PROD" \
    -T docs/Kbase/Presentations/KBaseArchitectureNarratives/KBaseArchitecture.zip \
    "https://kbase.us/services/shock-api/node?filename=KBaseArchitecture.zip&format=zip"
  "data": {
    "attributes": null,
    "created_on": "2024-02-01T21:27:36.000Z",
    "file": {
      "checksum": {
        "md5": "113f495f8f9d56ab642d860b5a15f535"
      },
      "name": "KBaseArchitecture.zip",
      "size": 3771041
    },
    "format": "",
    "id": "e3734583-10e1-4487-a1b8-1936f9ae6c11",
    "last_modified": "2024-02-01T21:27:36.000Z"
  },
  "error": null,
  "status": 200
}
  • Create a handle for the node
In [1]: with open("/home/crushingismybusiness/.kbase_token_prod") as f: 
   ...:     prodtoken = f.read().strip() 
   ...:                                                                         

In [2]: from biokbase.workspace.baseclient import BaseClient 

In [4]: bc = BaseClient("https://kbase.us/services/handle_service", token=prodto
   ...: ken)

In [9]: bc.call_method("AbstractHandle.persist_handle", [{ 
   ...:     "file_name": "KBaseArchitecture.zip", 
   ...:     "id": "e3734583-10e1-4487-a1b8-1936f9ae6c11", 
   ...:     "type": "shock", 
   ...:     "url": "https://kbase.us/services/shock-api/node/e3734583-10e1-4487-
   ...: a1b8-1936f9ae6c11", 
   ...:     "remote_md5": "113f495f8f9d56ab642d860b5a15f535" 
   ...: }])                                                                     
Out[9]: 'KBH_8142285'

Save a workspace report object with the handle:

In [11]: from biokbase.workspace.client import Workspace                        

In [12]: prodwsurl = "https://kbase.us/services/ws"                             

In [13]: prodws = Workspace(prodwsurl, token=prodtoken)                         

In [14]: ws = prodws                                                            

In [15]: ws.ver()                                                               
Out[15]: '0.14.2'

In [16]: ws.save_objects({"id": 46050, "objects": [{ 
    ...:     "name": "images_with_source", 
    ...:     "type": "KBaseReport.Report", 
    ...:     "data": { 
    ...:         "html_links": [{ 
    ...:             "URL": "https://kbase.us/services/shock-api/node/e3734583-1
    ...: 0e1-4487-a1b8-1936f9ae6c11", 
    ...:             "handle": "KBH_8142285", 
    ...:             "name": "images_with_source", 
    ...:          }], 
    ...:          "objects_created": [], 
    ...:          "text_message": "", 
    ...:      } 
    ...:  }]})                                                                  
Out[16]: 
[[3,
  'images_with_source',
  'KBaseReport.Report-2.0',
  '2024-02-01T22:08:45+0000',
  1,
  'gaprice',
  46050,
  'gaprice:narrative_1562895699854',
  '5680653aeb977710ae595a196ee2deca',
  194,
  {'Size(characters)': '0', 'Objects Created': '0'}]]
  • Now you can access the images in Narrative markdown via the current HTMLFileSetServ dynamic service url, the UPA of the object, and the filename of interest in the zip. See the HFFS docs for more information.
<img src="https://kbase.us/dynserv/66e9a37e7d3a56b64cc372c89d1eaafaa605c397.HTMLFileSetServ/api/v1/46111/4/1/$/0/Data%20Services_Inkscape_240131.png"
     alt="Data Services" width="50%"/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment