My app is working fine and download files locally when i am deploying the app on heroku server not able to download files how can i fix it

here is my code i can able to upload and download files on locally unable to download files after deployement
Preformatted textimport base64
import datetime
import io
from dash.dependencies import Input, Output, State
from dash import html, Dash
import plotly.express as px
from dash import dcc
import pandas as pd
from dash.dash import no_update
import dash_auth
VALID_USERNAME_PASSWORD_PAIRS = {
‘user’: ‘password’
}
external_stylesheets = [‘https://codepen.io/chriddyp/pen/bWLwgP.css’]
app = Dash(name, external_stylesheets=external_stylesheets)
server=app.server
auth = dash_auth.BasicAuth(
app,
VALID_USERNAME_PASSWORD_PAIRS
)

app.layout = html.Div([
html.H1(‘Convertion of FMS logdata to parced data’),

dcc.Upload(
    id='upload-data',
    children=html.Div([
        'Drag and Drop or ',
        html.B('Select Files')
    ]),
    style={
        'width': '100%',
        'height': '60px',
        'lineHeight': '60px',
        'borderWidth': '1px',
        'borderStyle': 'dashed',
        'borderRadius': '5px',
        'textAlign': 'center',
        'margin': '10px'
    },
    # Allow multiple files to be uploaded
    multiple=True
),
html.Div(id='output-data-upload'),
html.Div(
    [
        html.Button("download", id="btn"),
        dcc.Download(id="download")])],className='container')

def parse_contents(contents, filename, date):
content_type, content_string = contents.split(‘,’)
decoded = base64.b64decode(content_string)
global dh3
try:
if ‘csv’ in filename:
# Assume that the user uploaded a CSV file
df = pd.read_csv(
io.StringIO(decoded.decode(‘utf-8’)))
elif ‘xls’ in filename:
df = pd.read_excel(io.BytesIO(decoded), skiprows=10, nrows=16729, usecols=[12], header=None)
df2 = pd.read_excel(io.BytesIO(decoded), skiprows=8, usecols=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11])

        dfn = df.applymap(lambda x: x.replace(':::5548###[', ' ') if isinstance(x, str) else x)
        dfm = dfn.applymap(lambda x: x.replace(' ]]######', '') if isinstance(x, str) else x)
        dfl = dfm.applymap(lambda x: x.replace('###', '') if isinstance(x, str) else x)
        dfk = dfl.applymap(lambda x: x.replace(',', ' ') if isinstance(x, str) else x)
        df1 = df[12].str.extract(
            r"\[Priority=(\d*)\] \[GPS element=\[X=(-?\d*)\] \[Y=(-?\d*)\] \[Speed=(-?\d*)\] \[Angle=(-?\d*)\] \[Altitude=(-?\d*)\] \[Satellites=(-?\d*)\]]")
        df1.columns = ["Priority", "X", "Y", "Speed", "Angle", "Altitude", "Satellites"]
        df3 = dfk[12].str.extract(r"\[Timestamp=(-?\d*)\]")
        df4 = dfk[12].str.extract(r"\[EventSource=(-?\d*)\]")
        df5 = dfk[12].str.extract(r"\[IMEI = (-?\d*)\]")
        df6 = dfk[12].str.extract(r"\[IMEI = (-?\d*)\]")
        df7 = dfk[12].str.extract(r"\[Latitude = (-?\d*)\]")
        df8 = dfk[12].str.extract(r"\[Longitude = (-?\d*)\]")
        df9 = dfk[12].str.extract(r"\[Speed = (-?\d*)\]")
        df10 = dfk[12].str.extract(r"\[Angle = (-?\d*)\]")
        df11 = dfk[12].str.extract(r"\[Altitude = (-?\d*)\]")
        df12 = dfk[12].str.extract(r"\[Odometer = (-?\d*)\]")
        df13 = dfk[12].str.extract(r"\[IO = \[Power = (-?\d*)\]")
        df14 = dfk[12].str.extract(r"\[Digital Input 1 = (-?\d*)\]")
        df15 = dfk[12].str.extract(r"\[Digital Input 2 = (-?\d*)\]")
        df16 = dfk[12].str.extract(r"\[Battery Current = (-?\d*)\]")
        df17 = dfk[12].str.extract(r"\[Analog Input 2 = (-?\d*)\]")
        df18 = dfk[12].str.extract(r"\[Sleep Mode = (-?\d*)\]")
        df19 = dfk[12].str.extract(r"\[Analog Input 1 = (-?\d*)\]")
        df20 = dfk[12].str.extract(r"\[ICCID = (-?\d*)\]")
        df21 = dfk[12].str.extract(r"\[ICCID2 = (-?\d*)\]")
        df22 = dfk[12].str.extract(r"\[Axis X = (-?\d*)\]")
        df23 = dfk[12].str.extract(r"\[Asis Y = (-?\d*)\]")
        df24 = dfk[12].str.extract(r"\[Asis Z = (-?\d*)\]")
        df25 = dfk[12].str.extract(r"\[Data Mode = (-?\d*)\]")
        df26 = dfk[12].str.extract(r"\[PDOP = (-?\d*)\]")
        df27 = dfk[12].str.extract(r"\[Speed = (-?\d*)\]")
        df28 = dfk[12].str.extract(r"\[External Voltage = (-?\d*)\]")
        df29 = dfk[12].str.extract(r"\[Battery Voltage = (-?\d*)\]")
        df30 = dfk[12].str.extract(r"\[GSM Signal = (-?\d*)\]")
        df31 = dfk[12].str.extract(r"\[GPS Validity = (-?\d*)\]")
        df32 = dfk[12].str.extract(r"\[Battery Percentage = (-?\d*)\]")
        df33 = dfk[12].str.extract(r"\[Ignition = (-?\d*)\]")
        df34 = dfk[12].str.extract(r"\[Movement = (-?\d*)\]")
        df35 = dfk[12].str.extract(r"\[Active GSM Operator = (-?\d*)\]")
        df36 = dfk[12].str.extract(r"\[Digital Output 1 = (-?\d*)\]")
        df37 = dfk[12].str.extract(r"\[Digital Output 2 = (-?\d*)\]")
        df38 = dfk[12].str.extract(r"\[Voltage = (-?\d*)\]")
        df39 = dfk[12].str.extract(r"\Device Time : (-?\d*-?\d*-?\d* \d*:\d*:\d*)")
        df3.columns = ["Time_stamp"]
        df4.columns = ["EventSource"]
        df5.columns = ["IMEI"]
        df6.columns = ["IMEI"]
        df7.columns = ["Latitude"]
        df8.columns = ["Longitude"]
        df9.columns = ["Speed"]
        df10.columns = ["Angle"]
        df11.columns = ["Altitude"]
        df12.columns = ["Odometer"]
        df13.columns = ["IO(Power)"]
        df14.columns = ["Digital input(1)"]
        df15.columns = ["Digital input(2)"]
        df16.columns = ["Battery Current"]
        df17.columns = ["Analog Input 2 "]
        df18.columns = ["Sleep Mode"]
        df19.columns = ["Analog Input 1 "]
        df20.columns = ["ICCID"]
        df21.columns = ["ICCID2"]
        df22.columns = ["Axis X"]
        df23.columns = ["Axis Y"]
        df24.columns = ["Axis Z"]
        df25.columns = ["Data Mode"]
        df26.columns = ["PDOP"]
        df27.columns = ["Speed"]
        df28.columns = ["External Voltage"]
        df29.columns = ["Battery Voltage"]
        df30.columns = ["GSM Signal"]
        df31.columns = ["GPS Validity"]
        df32.columns = ["Battery Percentage"]
        df33.columns = ["Ignition"]
        df34.columns = ["Movement"]
        df35.columns = ["Active GSM Operator"]
        df36.columns = ["Digital Output 1"]
        df37.columns = ["Digital Output 2"]
        df38.columns = ["Voltage "]
        df39.columns = ["Device Time"]
        dataframe = pd.concat(
            [df2, df1, df3, df4, df5, df6, df7, df8, df9, df10, df11, df12, df13, df14, df15, df16, df17, df18,
             df19,
             df20, df21, df22, df23, df24, df25, df26, df27, df28, df29, df30, df31, df32, df33, df34, df35, df36,
             df37, df38, df39], axis=1)

        dh1 = dataframe.applymap(lambda x: x.replace("On", '1') if isinstance(x, str) else x)
        dh2 = dh1.applymap(lambda x: x.replace("Immobilize=", '') if isinstance(x, str) else x)
        dh2["Time_stamp"] = dh2["Time_stamp"].apply(pd.to_numeric)
        dh2["Analog Input 2 "] = dh2["Analog Input 2 "].apply(pd.to_numeric)
        dh2["Analog Input 2 "] = dh2["Analog Input 2 "].astype(float) * 0.001
        dh3 = dh2.applymap(lambda x: x.replace("Off", '0') if isinstance(x, str) else x)
except Exception as e:
    print(e)
    return html.Div([
        'There was an error processing this file.'
     ])

return html.Div([
    html.P('Analog Input2 with Respect to Timestamp'),
    dcc.Graph(
        figure=px.line(dh3,x='Time_stamp',y='Analog Input 2 ',template="plotly_dark").update_layout(
{"plot_bgcolor": "rgba(0, 0, 0, 0)", "paper_bgcolor": "rgba(0, 0, 0, 0)"}),


    ),

        ])

@app.callback(
Output(“download”, “data”),
[Input(“btn”, “n_clicks”)],
prevent_initial_call=True,
)
def func(n_clicks):
return dcc.send_data_frame(dh3.to_excel, “new parced excel data.xlsx”)
@app.callback(Output(‘output-data-upload’,‘children’),
Input(‘upload-data’, ‘contents’),
State(‘upload-data’, ‘filename’),
State(‘upload-data’, ‘last_modified’))
def update_output(list_of_contents, list_of_names, list_of_dates):
if list_of_contents is not None:
children = [
parse_contents(c, n, d) for c, n, d in
zip(list_of_contents, list_of_names, list_of_dates)]
return children

if name == ‘main’:
app.run_server(debug=True,port=4)
`

1 Like

I have same issue, so instead of download, I used export option from dash_table.DataTable.

1 Like

ok thank you

can we deploy dash app on our own pc server???

1 Like

Yes. It is possible. Although, it is normally better to have something dedicated to run it.

how can i do that can you please tell me

Sure. If you are on windows you can do a task scheduler to run your venv and then use waitress to serve your website automatically.

https://flask.palletsprojects.com/en/2.2.x/deploying/waitress/

But… if you have an older computer lying around you can put Proxmox on it and run Linux containers on your home network. This would use gunicorn and nginx as a reverse proxy.

Thanks. I will try it

1 Like