Skip to content

Update filetable.py example to make it work #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 18, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 56 additions & 39 deletions examples/filetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import firefly_client


def filetable_to_firefly(ffclient, topdir, pattern, recursive=True):
"""Upload table of files matching a pattern to a FireflyClient

Expand All @@ -15,80 +16,96 @@ def filetable_to_firefly(ffclient, topdir, pattern, recursive=True):
ffclient: firefly_client.FireflyClient
Instance of FireflyClient connected to a Firefly server

topdir: 'str'
topdir: "str"
pathname for directory to search

pattern: 'str'
filename pattern to search for, e.g. '*.fits'
pattern: "str"
filename pattern to search for, e.g. "*.fits"

recursive: `bool`
Search all subdirectories recursively (default=True)

Returns:
--------
tbl_val: 'str'
tbl_val: "str"
Descriptor of table that was uploaded to the Firefly server

metadict: `dict`
Dictionary of metadata items

"""
filelist = glob(topdir+'/**/' + pattern, recursive=recursive)
metadict = {'datasource': 'path'}
with tempfile.NamedTemporaryFile(mode='w+t',
delete=False,
suffix='.csv') as fd:
filelist = sorted(glob(topdir + "/**/" + pattern, recursive=recursive))
metadict = {"datasource": "path"}
with tempfile.NamedTemporaryFile(mode="w+t", delete=False, suffix=".csv") as fd:
csv_writer = csv.writer(fd)
csv_writer.writerow(['number','name','path'])
csv_writer.writerow(["number", "name", "path"])
for i, path in enumerate(filelist):
csv_writer.writerow([i, os.path.basename(path),
'file://'+path])
# Docker Firefly allows uploads from /external
csv_writer.writerow([i,
os.path.basename(path),
"file:///external" + path])

tbl_val = ffclient.upload_file(fd.name)
os.remove(fd.name)
return(tbl_val, metadict)
return (tbl_val, metadict)


# Sample application
def main():
import argparse
parser = argparse.ArgumentParser(description="""

parser = argparse.ArgumentParser(
description="""
Display a table of files in a Firefly window
""")
parser.add_argument('topdir', help='top-level directory to search')
parser.add_argument('pattern', help='filename pattern for search')
parser.add_argument('--norecursion', help='do not recursively search topdir',
action='store_true')
parser.add_argument('--host', help='host address for Firefly',
default='localhost')
parser.add_argument('--port', help='port for Firefly', default='8080')
parser.add_argument('--base', help='base.for Firefly', default='firefly')
parser.add_argument('--channel', help='channel name for websocket',
default=None)
parser.add_argument('--printurl', help='print browser url instead of' +
' attempting to launch browser', action='store_true')
"""
)
parser.add_argument("topdir", help="top-level directory to search")
parser.add_argument("pattern", help="filename pattern for search")
parser.add_argument(
"--norecursion", help="do not recursively search topdir", action="store_true"
)
parser.add_argument(
"--firefly_url", help="URL for Firefly server", default=os.getenv("FIREFLY_URL")
)
parser.add_argument("--channel", help="channel name for websocket", default=None)
parser.add_argument(
"--printurl",
help="print browser url instead of" + " attempting to launch browser",
action="store_true",
)

args = parser.parse_args()
topdir = args.topdir
pattern = args.pattern
host = args.host
port = args.port
basedir = args.base
firefly_url = args.firefly_url
channel = args.channel
printurl = args.printurl
launch_browser = True
if printurl:
launch_browser = False
recursion = not args.norecursion

fc = FireflyClient.make_client(url='{}:{}'.format(host, port), channel_override=channel, html_file='slate.html')
fc = firefly_client.FireflyClient.make_client(
url=firefly_url,
channel_override=channel,
html_file="slate.html",
launch_browser=launch_browser,
)
if printurl:
print("Firefly URL is {}".format(fc.get_firefly_url()))

print("Searching for files...", end="")
tbl_val, metainfo = filetable_to_firefly(fc, topdir, pattern, recursive=recursion)
print("done.")

if printurl:
print('Firefly url is {}'.format(fc.get_firefly_url()))
input("Press Enter after your browser is open to the Firefly URL...")

tbl_val, metainfo = filetable_to_firefly(fc, topdir, pattern,
recursive=recursion)
r = fc.add_cell(0, 0, 1, 2, 'tables', 'main')
r = fc.add_cell(0, 0, 1, 2, "tables", "main")
fc.show_table(tbl_val, meta=metainfo)
r = fc.add_cell(0, 1, 1, 2, 'tableImageMeta', 'image-meta')
fc.show_image_metadata(viewer_id=r['cell_id'])
r = fc.add_cell(0, 1, 1, 2, "tableImageMeta", "image-meta")
fc.show_image_metadata(viewer_id=r["cell_id"])

if __name__ == '__main__':
main()

if __name__ == "__main__":
main()