Dash Plotly - Not showing full list of files from a folder on multi tabs

﹥>﹥吖頭↗ 提交于 2021-01-29 17:45:00

问题


I have a requirement of downloading files from a folder which I could do using this link

After which I wanted to segment the files in different tabs which I could do but NOT ALL FILES are listed. Only shows one file out of 10 files

Any hints will help.

I want to cluster similar type of files in each tab for user to download as per the segment and it should show all the files in that folder.

I could see full list when tabs are not added. But after adding tabs to the dash code the full file list is not showing. I tried changing the height, width but no luck.

File format is csv

Below is the code sample:

from urllib.parse import quote as urlquote

from flask import Flask, send_from_directory
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

server = Flask(__name__)
api_host='xxx-xxx-xxx.xx.xxx.com'
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(server=server, external_stylesheets=external_stylesheets)

app.config['suppress_callback_exceptions'] = True

tab_height = '5vh'
app.layout = html.Div([
    html.H1('Decentralized App for QVR File share'),
    html.Div([
        dcc.Tabs(id="tabs-ds", value='tab-ds', style={'height':tab_height}, children=[
        dcc.Tab(label='i-SAC', value='isac', style={'padding': '0','line-height': tab_height},selected_style={'padding': '0','line-height': tab_height}),
        dcc.Tab(label='GSNOW', value='gsnow',style={'padding': '0','line-height': tab_height},selected_style={'padding': '0','line-height': tab_height})
        ]),
        html.Div(id='tabs-content-ds')
])
])

@app.callback(Output('tabs-content-ds', 'children'),
              [Input('tabs-ds', 'value')])

def render_content(tab):
    if tab == 'isac':
        return tab_isac.tab_isac
    elif  tab == 'gsnow':
        return tab_gsnow.tab_gsnow

#tab_isac callback section--
#----------------------------------------------------------------

ISAC_UPLOAD_DIRECTORY = "/xxxx/aaa/bb/aaa"

@server.route("/download/<path:path>")
def download(path):
    """Serve a file from the upload directory."""
    return send_from_directory(ISAC_UPLOAD_DIRECTORY, path, as_attachment=True)

def save_file_isac(name_isac, content):
    """Decode and store a file uploaded with Plotly Dash."""
    data_isac = content.encode("utf8").split(b";base64,")[1]
    with open(os.path.join(ISAC_UPLOAD_DIRECTORY, name_isac), "wb") as fp:
        fp.write(base64.decodebytes(data_isac))

def uploaded_files_isac():
    """List the files in the upload directory."""
    files_isac = []
    for filename_isac in os.listdir(ISAC_UPLOAD_DIRECTORY):
        path = os.path.join(ISAC_UPLOAD_DIRECTORY, filename_isac)
        if os.path.isfile(path):
            files_isac.append(filename_isac)
        return files_isac

def file_download_link(filename_isac):
    """Create a Plotly Dash 'A' element that downloads a file from the app."""
    location = "/download/{}".format(urlquote(filename_isac))
    return html.A(filename_isac, href=location)

@app.callback(dash.dependencies.Output("isac_qvr-file-list-download", "children"), [dash.dependencies.Input("isac-upload-data", "filename"), Input("isac-upload-data", "contents")],)

def update_output_download(uploaded_filenames_isac, uploaded_file_contents_isac):
    """Save uploaded files and regenerate the file list."""
    if uploaded_filenames_isac is not None and uploaded_file_contents_isac is not None:
        for name_isac, data_isac in zip(uploaded_filenames_isac, uploaded_file_contents_isac):
            save_file_isac(name_isac, data_isac)
    files = uploaded_files_isac()
    if len(files) == 0:
        return [html.Li("No files yet!")]
    else:
        return [html.Li(file_download_link(filename)) for filename in files]
       

if __name__ == "__main__":
    app.run_server(debug=True, host=api_host)

来源:https://stackoverflow.com/questions/63318261/dash-plotly-not-showing-full-list-of-files-from-a-folder-on-multi-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!