Is it possible to export dash datatable to a specific location on disk or directly to SQL Server?

大城市里の小女人 提交于 2020-07-09 05:26:24

问题


I created a tool in Dash where user pulls data from SQL Server, filters out desired rows and edits values in one of the columns. Now I have to get that filtered and edited table in a new table in SQL Server.

I'm new to dash and can't find a way to export directly to SQL Server or export .csv to a specific location on disc and handle it with a SQL Server procedure from there. Has anyone had a problem like this and knows if it is even possible?

I managed to export .csv, but it goes into the downloads folder.


回答1:


Here is an example i created.

The "Save" button will save to "H://R//filename.csv", so replace this in the code with your desired file location/name.

import dash
import dash_table
import dash_html_components as html
from dash.dependencies import Input, Output, State
import pandas as pd
from dash.exceptions import PreventUpdate

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/solar.csv')

app = dash.Dash(__name__)

app.layout = html.Div([
            dash_table.DataTable(
                id='table',
                columns=[{"name": i, "id": i} for i in df.columns],
                data=df.to_dict("rows"),
                editable=True
            ),
            html.Button(id="save-button",n_clicks=0,children="Save"),
            html.Div(id="output-1",children="Press button to save changes")
])

@app.callback(
        Output("output-1","children"),
        [Input("save-button","n_clicks")],
        [State("table","data")]
        )

def selected_data_to_csv(nclicks,table1): 
    if nclicks == 0:
        raise PreventUpdate
    else:
        pd.DataFrame(table1).to_csv('H://R//filename.csv',index=False)
        return "Data Submitted"

if __name__ == '__main__':
    app.run_server(debug=True)


来源:https://stackoverflow.com/questions/57649199/is-it-possible-to-export-dash-datatable-to-a-specific-location-on-disk-or-direct

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