
import os
os.environ['POLLINATION_API_URL'] = 'https://api.staging.pollination.cloud'

import streamlit.components.v1 as components
from pollination_streamlit.selectors import get_api_client
from pollination_streamlit_io import (create_study, study_progress, select_cloud_artifact)

from pollination_streamlit_viewer import viewer
import streamlit as st

api_client = get_api_client()

study_info = create_study('st-study',
                          api_client=api_client)

if study_info:
    # click on folder card or file card to get output
    name, value, type = study_progress(api_client=api_client,
                                 study_info = study_info,
                                 show_output=True)
    # if folder card explore selected folder
    if value and type == 'folder':
        # cloud only
        name, value, type = select_cloud_artifact(api_client=api_client,
                              project_name=study_info.get('projectName'),
                              project_owner=study_info.get('accountName'),
                              study_id=study_info.get('studyId'),
                              file_name_match='.*(txt|json)',
                              path=value)
        # read txt or json
        if type == 'file':
            st.text(value.decode('utf-8'))
        
    # if file card read the file
    elif value and type == 'file' and name.endswith('vtkjs'):
        viewer(key='result', content=value)

