Deploy a Python (Dash) app to Heroku using Conda environments (instead of virtualenv)

后端 未结 1 1117
暗喜
暗喜 2020-12-03 08:51

When I run git push heroku master to deploy my app to Heroku I keep getting errors

Heroku Push rejected, failed to compile Python app. Co

相关标签:
1条回答
  • 2020-12-03 09:46

    Heroku doesn't care if you're using virtualenv or conda to manage environments. Using one or the other is mostly irrelevant to the deployment process.

    Don't bother with the Conda Environment Buildpack instructions since those are for deploying a remote conda environment which is not what you are trying to do. You, my friend, are trying to deploy a remote your_app environment.

    Here's how to do that with a dash application and conda:

    Create a new folder for your project:

    $ mkdir dash_app_example
    $ cd dash_app_example
    

    Initialize the folder with git

    $ git init # initializes an empty git repo
    

    Create an environment.yml file in dash_app_example:

    name: dash_app #Environment name
    dependencies:
      - python=3.6
      - pip:
        - dash
        - dash-renderer
        - dash-core-components
        - dash-html-components
        - plotly
        - gunicorn # for app deployment
    

    Create the environment from environment.yml:

    $ conda env create
    

    Activate the conda environment

    $ source activate dash_app #Writing source is not required on Windows
    

    Confirm that the environment you're in is correct.

    It should currently be in dash_app:

    $ conda info --envs #Current environment is noted by a *
    

    Initialze the folder with app.py, requirements.txt, and a Procfile:


    app.py

    import dash
    import dash_core_components as dcc
    import dash_html_components as html
    import os
    
    app = dash.Dash(__name__)
    server = app.server
    
    app.css.append_css({"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"})
    
    app.layout = html.Div([
        html.H2('Hello World'),
        dcc.Dropdown(
            id='dropdown',
            options=[{'label': i, 'value': i} for i in ['LA', 'NYC', 'MTL']],
            value='LA'
        ),
        html.Div(id='display-value')
    ])
    
    @app.callback(dash.dependencies.Output('display-value', 'children'),
                  [dash.dependencies.Input('dropdown', 'value')])
    def display_value(value):
        return 'You have selected "{}"'.format(value)
    
    if __name__ == '__main__':
        app.run_server(debug=True)
    

    Procfile

    web: gunicorn app:server
    

    requirements.txt: describes your Python dependencies. You can fill this file in automatically by running $ pip freeze > requirements.txt on the command line.


    Your folder structure should look like

    - dash_app_example
    --- app.py
    --- environment.yml
    --- Procfile
    --- requirements.txt
    

    Notice how there's no environment data in this directory. That's because conda unlike virtualenv stores all your environments in one place neatly away from your app directory. There's no need to .gitignore those files... they're not here!

    Initialize Heroku, add files to Git, and deploy

    $ heroku create my-dash-app # change my-dash-app to a unique name
    $ git add . # add all files to git
    $ git commit -m 'Initial app boilerplate'
    $ git push heroku master # deploy code to heroku
    $ heroku ps:scale web=1  # run the app with a 1 heroku "dyno"
    

    Sources:

    1. Deploying an application with Heroku (using Conda Environments)
    2. My Python Environment Workflow with Conda
    3. Deploying Dash Apps (using virtualenv)
    0 讨论(0)
提交回复
热议问题