Heroku 不在乎您是使用 virtualenv 还是 conda 来管理环境。使用其中一种与部署过程几乎无关。
不要理会Conda Environment Buildpack 说明,因为这些说明用于部署远程conda 环境,这不是您想要做的。您,我的朋友,正在尝试部署远程 your_app 环境。
为您的项目创建一个新文件夹:
$ mkdir dash_app_example
$ cd dash_app_example
用 git 初始化文件夹
$ git init # initializes an empty git repo
在dash_app_example中创建一个environment.yml文件:
name: dash_app #Environment name
dependencies:
- python=3.6
- pip:
- dash
- dash-renderer
- dash-core-components
- dash-html-components
- plotly
- gunicorn # for app deployment
从environment.yml创建环境:
$ conda env create
激活 conda 环境
$ source activate dash_app #Writing source is not required on Windows
确认你所处的环境是正确的。
它当前应该在 dash_app 中:
$ conda info --envs #Current environment is noted by a *
使用app.py、requirements.txt 和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:描述您的 Python 依赖项。您可以通过在命令行上运行$ pip freeze > requirements.txt 来自动填写此文件。
你的文件夹结构应该是这样的
- dash_app_example
--- app.py
--- environment.yml
--- Procfile
--- requirements.txt
请注意此目录中没有环境数据。那是因为conda 不像virtualenv 将你所有的环境存储在一个远离你的应用程序目录的地方。没有必要.gitignore那些文件......他们不在这里!
初始化 Heroku,将文件添加到 Git,然后部署
$ 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"
来源:
- Deploying an application with Heroku (using Conda Environments)
- My Python Environment Workflow with Conda
-
Deploying Dash Apps(使用
virtualenv)