Skip to content

Commit 7562125

Browse files
Create a show_data method to allow any upload
1 parent decff5e commit 7562125

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

firefly_client/fc_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def ensure3(val, name):
9595
'ShowHiPS': 'ImagePlotCntlr.PlotHiPS',
9696
'ShowImageOrHiPS': 'ImagePlotCntlr.plotHiPSOrImage',
9797
'ImagelineBasedFootprint': 'DrawLayerCntlr.ImageLineBasedFP.imagelineBasedFPCreate',
98+
'ShowAnyData': 'app_data.externalUpload',
9899

99100
# actions from jupyter_firefly_extensions
100101
'StartLabWindow': 'StartLabWindow',

firefly_client/firefly_client.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
This module defines class 'FireflyClient' and methods to remotely communicate to Firefly viewer
55
by dispatching remote actions.
66
"""
7+
import io
8+
import re
79
import requests
810
import webbrowser
911
import json
@@ -12,6 +14,7 @@
1214
from urllib.parse import urljoin
1315
import math
1416
import weakref
17+
import os
1518
from copy import copy
1619

1720

@@ -729,6 +732,55 @@ def reinit_viewer(self):
729732
Status of the request, like {'success': True}.
730733
"""
731734
return self.dispatch(ACTION_DICT['ReinitViewer'], {})
735+
736+
def get_file_payload(self, file_input):
737+
# TODO: debug displayName not appearing in some cases
738+
if isinstance(file_input, str):
739+
if file_input.startswith('$'):
740+
return {'fileOnServer': file_input}
741+
elif os.path.isfile(file_input):
742+
return {'fileOnServer': self.upload_file(file_input),
743+
'displayName': os.path.basename(file_input)}
744+
elif re.match(r'^https?://', file_input):
745+
return {'url': file_input,
746+
'displayName': os.path.basename(file_input)}
747+
elif isinstance(file_input, io.IOBase) and hasattr(file_input, 'seek'): # file-like object
748+
return {'fileOnServer': self.upload_data(file_input, 'UNKNOWN')}
749+
raise ValueError('file_input must be a valid file path string or a file-like object')
750+
751+
def show_data(self, file_input, preview_metadata=False,
752+
**data_view_options):
753+
"""
754+
Show any data file of the type that Firefly supports:
755+
- Custom catalog or table in IPAC, CSV, TSV, VOTABLE, Parquet, or FITS table format
756+
- A ds9 region file
757+
- Images in FITS format, including multi-extension FITS files with images, tables, or a mixture of both
758+
- A Multi-Order Coverage Map (MOC) in FITS format
759+
- A Data Link Table file
760+
- A UWS job file
761+
762+
Parameters
763+
----------
764+
file_input : `str` or `file-like object`
765+
If `str`, it can be a local file path, a URL to a remote file, or
766+
name of a file on the server (return value of `upload_file()`).
767+
Else it can be a file stream object (file-like object) opened in Python.
768+
preview_metadata : `bool`, optional
769+
If True, preview the metadata of the file before loading the data.
770+
This allows you to select FITS extensions, table columns, etc. and
771+
then you can click "Load" button in the UI to load your selections.
772+
Default is False.
773+
**data_view_options : optional keyword arguments
774+
Additional options for the view of this data file in the UI, such as:
775+
**displayName** : `str`, optional
776+
A name to display in the UI for the data file loaded.
777+
"""
778+
payload = {
779+
**self.get_file_payload(file_input),
780+
'immediate': not preview_metadata,
781+
**data_view_options # TODO: pass data_view_id -> didn't work
782+
}
783+
return self.dispatch(ACTION_DICT['ShowAnyData'], payload)
732784

733785
def show_fits_image(self, file_on_server=None, plot_id=None, viewer_id=None, **additional_params):
734786
"""

0 commit comments

Comments
 (0)