<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Erik Billebjer Ulrikson</title>
    <description>The latest articles on Forem by Erik Billebjer Ulrikson (@ulrikson).</description>
    <link>https://forem.com/ulrikson</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F514440%2Fd9fa6e6b-e2ec-4b6c-80d5-ff8ef187a8e1.jpeg</url>
      <title>Forem: Erik Billebjer Ulrikson</title>
      <link>https://forem.com/ulrikson</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ulrikson"/>
    <language>en</language>
    <item>
      <title>Deploying a Flask + Vue app to Heroku</title>
      <dc:creator>Erik Billebjer Ulrikson</dc:creator>
      <pubDate>Sun, 15 Nov 2020 10:07:57 +0000</pubDate>
      <link>https://forem.com/ulrikson/deploying-a-flask-vue-app-to-heroku-3bf8</link>
      <guid>https://forem.com/ulrikson/deploying-a-flask-vue-app-to-heroku-3bf8</guid>
      <description>&lt;p&gt;In this post, I'll walk you through how to deploy a Flask and Vue app to Heroku. The final product is a super basic project you can extend to your needs.&lt;/p&gt;

&lt;p&gt;Inspiration was drawn from &lt;a href="https://medium.com/@shaylandias_85978/deploying-a-flask-web-app-with-a-react-frontend-to-heroku-6168fc4ff2e5"&gt;this post by Shaylan Dias&lt;/a&gt; and has been adapted to Vue.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TLDR;&lt;/strong&gt; &lt;a href="https://github.com/ulrikson/heroku-test"&gt;Here's my GitHub Repo&lt;/a&gt; and &lt;a href="https://testing-flask-vue.herokuapp.com/"&gt;here's the final product&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 1: Create a basic Vue app
&lt;/h1&gt;

&lt;p&gt;We're using &lt;a href="https://cli.vuejs.org/guide/installation.html"&gt;Vue CLI&lt;/a&gt; for this project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir heroku-test
cd heroku-test

# Instantiate git
git init

# Vue CLI to create child folder
vue create client

cd client
npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you'll get a localhost link in your terminal. Visit that and view your frontend.&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 2: Create Flask server
&lt;/h1&gt;

&lt;p&gt;I assume you have pip installed for this. If not, &lt;a href="https://pip.pypa.io/en/stable/installing/"&gt;download pip&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Create a virtual Python environment for this project
python3 -m venv .venv

# Activate the virtual env
source .venv/bin/activate

# Install dependencies
pip install flask gunicorn python-dotenv

# Create requirements.txt
pip freeze &amp;gt; requirements.txt

# Set up the .env file
touch .env
echo "FLASK_APP=server.app:app" &amp;gt; .env

# Create the server
mkdir server
cd server
touch app.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you have to set up app.py, this is a basic script that'll do the job:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask 
from dotenv import load_dotenv
import os
load_dotenv()

# Set up the app and point it to Vue
app = Flask(__name__, static_folder='../client/dist/',    static_url_path='/')

# Set up the index route
@app.route('/')
def index():
    return app.send_static_file('index.html')

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 8000))
    app.run(host='0.0.0.0', port=port)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When setting upp the app (&lt;code&gt;app=...&lt;/code&gt;) it's crucial that it's pointed to Vues static files. When using Vue CLI, these can be found in the &lt;strong&gt;dist&lt;/strong&gt; folder. &lt;/p&gt;

&lt;h1&gt;
  
  
  Step 3: Setup for Heroku
&lt;/h1&gt;

&lt;p&gt;First, create a &lt;code&gt;package.json&lt;/code&gt;and &lt;code&gt;Procfile&lt;/code&gt; in the root of your project.&lt;/p&gt;

&lt;p&gt;The Procfile will tell Heroku what processes to run.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Procfile
web: npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And package.json specifies the project's dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# package.json
{
  "name": "heroku-flask-vue",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "core-js": "^3.6.5",
    "eslint": "^7.13.0",
    "vue": "^2.6.11"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "babel-eslint": "^10.1.0",
    "eslint": "^6.7.2",
    "eslint-plugin-vue": "^6.2.2",
    "vue-template-compiler": "^2.6.11"
  },
  "scripts": {
    "start": "gunicorn server.app:app",
    "build": "cd client &amp;amp;&amp;amp; npm run build"
  },
  "eslintConfig": {
    "extends": "plugin:vue/recommended"
  },
  "browserslist": {
    "production": [
      "&amp;gt;0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "engines": {
    "node": "12.x"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step 4: Create the Heroku app
&lt;/h1&gt;

&lt;p&gt;Now, we have to create a Heroku app. If you don't have Heroku CLI installed, follow &lt;a href="https://devcenter.heroku.com/articles/heroku-cli"&gt;these instructions&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku create &amp;lt;name-for-your-app&amp;gt;

# create a remote git connection
heroku git:remote -a &amp;lt;name-for-your-app&amp;gt;

# These buildpack tell Heroku to install Python and Node
heroku buildpacks:add --index 1 heroku/nodejs
heroku buildpacks:add --index 2 heroku/python
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step 5: Test locally and then deploy
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run build
npm start

# If it works, deploy!
git commit -am 'Ready to deploy!'
git push heroku master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Heroku CLI will give you a link when your app has been deployed, follow that and voilá!&lt;/p&gt;

&lt;h1&gt;
  
  
  Final
&lt;/h1&gt;

&lt;p&gt;Great job! You've created a Vue + Flask app and deployed it to Heroku. Now try changing for example the &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt;. PS. Don't forget to run &lt;code&gt;npm run build&lt;/code&gt; before pushing to Heroku again.&lt;/p&gt;

</description>
      <category>python</category>
      <category>vue</category>
      <category>devops</category>
      <category>heroku</category>
    </item>
  </channel>
</rss>
