[No need to build local environment] Deploy Python bottle application made with Cloud9 to Heroku

Introduction

This is a memo for myself, the details are as follows.

Prerequisites

What i did

[Cloud9] Creating a workspace

https://c9.io/new Create workspace by specifying Python in Choose a template image

[Cloud9] Python version upgrade

By default, Cloud9 has a Python version of Python2, so change it to use Python3.5.1. Press the green + button at the bottom of the screen to open a terminal with [New Terminal] and type commands there. image

#Check the version before change
$ python --version
Python 2.7.6

#Save the Python2 system
$ sudo mv /usr/bin/python /usr/bin/python2

#Python3.Link to 5
$ sudo ln -s /usr/bin/python3.5 /usr/bin/python

#Confirm that the version has changed
$ python --version
Python 3.5.1

[Cloud9] Introduction of bottle

If it is as it is, the bottle to be used this time is not included, so I will introduce it.

$ sudo pip install bottle
Downloading/unpacking bottle
  Downloading bottle-0.12.9.tar.gz (69kB): 69kB downloaded
  Running setup.py (path:/tmp/pip_build_root/bottle/setup.py) egg_info for package bottle
    
Installing collected packages: bottle
  Running setup.py install for bottle
    changing mode of build/scripts-3.5/bottle.py from 644 to 755
    
    changing mode of /usr/local/bin/bottle.py to 755
Successfully installed bottle
Cleaning up...

[Cloud9] Delete all files

For the time being, delete all the files that were originally generated. image

[Cloud9] Creating and coding app.py

Create a file with [Right click] => [New File], and enter app.py with [Right click] => [Rename] on the created file. image

Finally, we will implement app.py from here. First and foremost, when you access http: // {your-domain} / hello / on your browser, Hello World will be displayed.

app.py


# -*- coding:utf-8 -*-

import os
from bottle import route, run


@route('/hello/')
def hello():
    return "Hello World"


run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000)))

[Cloud9] Try running

When you press the Run button and execute it, it will tell you the URL to view in the browser, so open it. At this time, don't forget to add "/ hello /" with the routing set at the end. image

It was safely displayed in the browser !! image

[Cloud9] Commit to GitHub

Once you've done this, commit to GitHub.

First, create a new repository on GitHub. https://github.com/new

You need to register your SSH key to commit from Cloud9 to GitHub. From the Cloud9 dashboard, register the public key displayed in [Gear mark] => [SSH Keys] on the upper right to the linked GitHub. https://c9.io/account/ssh => https://github.com/settings/ssh

After registering the key, push it on the Cloud9 terminal.


#Pay attention to the directory to execute
$ pwd
/home/ubuntu/workspace

#Generate files required for deploy
$ pip freeze | grep bottle > requirements.txt #It seems better to put only the package you are using
$ python --version | sed 's/\ /-/g' |sed 's/P/p/g' > runtime.txt
$ echo web: python app.py > Procfile 

#The following is for reference only, please copy the command displayed on the screen of the newly created repository as appropriate.
$ echo "# cloud9-bottle-heroku" >> README.md
git init
git add .
git commit -m "first commit"
git remote add origin https://github.com/path/to-youre-repository.git #Please note that it depends on the person
git push -u origin master

[Cloud9] deploy to Heroku

Next, we will reflect it on heroku, and continue to work on the Cloud9 terminal. Heroku toolbelt seems to have been installed in advance, so no addition is necessary.

#First, log in to heroku.
$ heroku login

#Create app(Please change as appropriate so that the name is not covered)
$ heroku create cloud9-bottle-heroku
Creating cloud9-bottle-heroku... done, stack is cedar-14
https://cloud9-bottle-heroku.herokuapp.com/ | https://git.heroku.com/cloud9-bottle-heroku.git

$ git push heroku master
Counting objects: 21, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (15/15), done.
Writing objects: 100% (21/21), 1.95 KiB | 0 bytes/s, done.
Total 21 (delta 5), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Python app detected
remote: -----> Installing python-3.5.1
remote:      $ pip install -r requirements.txt
remote:        Collecting bottle==0.12.9 (from -r requirements.txt (line 1))
remote:          Downloading bottle-0.12.9.tar.gz (69kB)
remote:        Installing collected packages: bottle
remote:          Running setup.py install for bottle: started
remote:            Running setup.py install for bottle: finished with status 'done'
remote:        Successfully installed bottle-0.12.9
remote: 
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: 
remote: -----> Compressing...
remote:        Done: 49M
remote: -----> Launching...
remote:        Released v3
remote:        https://cloud9-bottle-heroku.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
To https://git.heroku.com/cloud9-bottle-heroku.git
 * [new branch]      master -> master

I was able to open it in my browser !! https://cloud9-bottle-heroku.herokuapp.com/hello/ image

[Heroku] GitHub cooperation

If you push to GitHub, change the setting so that it will be automatically deployed to heroku. Select the target application from the Heroku dashboard and select Deploy at the top of the screen. image

Select GitHub in the Deployment method, enter the repository name in Connect to GitHub, search, and Connect where the target repository is displayed. image

Select the target branch as appropriate and press Enable Automatic Deploys to complete the setting. image

You can check if the automatic Deploy works from Activity at the top of the screen. Try pushing an empty commit from the Cloud9 terminal and see if Deploy is actually done.

 $ git commit --allow-empty -m "deploy test"
[master 9912982] deploy test

$ git push origin master

If you wait about 1 minute after typing the command and then open the Activity tab, you can see that Deploy has been executed. image

Now, if you push to GitHub without git push heroku master, the update will be reflected on Heroku side as well.

Where I was addicted

Contents of requirements.txt

It seems better to keep it to the minimum necessary.

#the first
$ pip freeze > requirements.txt

#I got angry with a dependency when I pushed to heroku
$ git push heroku master                                                                                                                                  
Counting objects: 18, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (18/18), 1.73 KiB | 0 bytes/s, done.
Total 18 (delta 4), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Python app detected
remote: -----> Installing python-3.5.1
remote:      $ pip install -r requirements.txt
remote:        Collecting Pillow==2.3.0 (from -r requirements.txt (line 1))
remote:          Downloading Pillow-2.3.0.zip (2.4MB)
remote:        Collecting bottle==0.12.9 (from -r requirements.txt (line 2))
remote:          Downloading bottle-0.12.9.tar.gz (69kB)
remote:        Collecting chardet==2.2.1 (from -r requirements.txt (line 3))
remote:          Downloading chardet-2.2.1-py2.py3-none-any.whl (180kB)
remote:        Collecting colorama==0.2.5 (from -r requirements.txt (line 4))
remote:          Downloading colorama-0.2.5.zip
remote:        Collecting decorator==3.4.0 (from -r requirements.txt (line 5))
remote:          Downloading decorator-3.4.0.tar.gz
remote:        Collecting html5lib==0.999 (from -r requirements.txt (line 6))
remote:          Downloading html5lib-0.999.tar.gz (885kB)
remote:        Collecting ipython==1.2.1 (from -r requirements.txt (line 7))
remote:          Downloading ipython-1.2.1.tar.gz (8.7MB)
remote:        Collecting matplotlib==1.3.1 (from -r requirements.txt (line 8))
remote:          Downloading matplotlib-1.3.1.tar.gz (42.7MB)
remote:        Collecting nose==1.3.1 (from -r requirements.txt (line 9))
remote:          Downloading nose-1.3.1.tar.gz (274kB)
remote:        Collecting numpy==1.8.2 (from -r requirements.txt (line 10))
remote:          Downloading numpy-1.8.2.tar.gz (3.8MB)
remote:        Collecting pycurl==7.19.3 (from -r requirements.txt (line 11))
remote:          Downloading pycurl-7.19.3.tar.gz (113kB)
remote:        Collecting pygobject==3.12.0 (from -r requirements.txt (line 12))
remote:          Could not find a version that satisfies the requirement pygobject==3.12.0 (from -r requirements.txt (line 12)) (from versions: 2.28.3)
remote:        No matching distribution found for pygobject==3.12.0 (from -r requirements.txt (line 12))
remote: 
remote:  !     Push rejected, failed to compile Python app
remote: 
remote: Verifying deploy....
remote: 
remote: !       Push rejected to cloud9-bottle-heroku.
remote: 
To https://git.heroku.com/cloud9-bottle-heroku.git
 ![remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/cloud9-bottle-heroku.git'

#It was okay if I squeezed it.
$ pip freeze | grep bottle > requirements.txt

Notation of runtime.txt

When I printed the result of python --version, it was python instead of python.

#Bad example
Python-3.5.1

#Good example
python-3.5.1

in conclusion

Thanks to Cloud9, it is not necessary to build a local environment, but for some reason the movement sometimes gets stuck, so in my environment development using PyCharm was a more comfortable impression. However, considering the case of developing with multiple terminals, I think that it is troublesome to arrange those development environments, so I think that there is an option to use in such a case. Since Heroku is using only the minimum this time, there are many parts that I do not understand well yet, and it seems that I need to sleep for 6 hours a day, but it is nice to be able to publish the application without detailed settings. ..

reference

Heroku Python Support | Heroku Dev Center : https://devcenter.heroku.com/articles/python-support Publish a web app in 4 minutes 33 seconds using Heroku x bottle --Qiita: http://qiita.com/ohbarye/items/55ec574f10685a012baf

Recommended Posts

[No need to build local environment] Deploy Python bottle application made with Cloud9 to Heroku
Until you publish (deploy) a web application made with bottle on Heroku
Web application made with Python3.4 + Django (Part.1 Environment construction)
Build a machine learning application development environment with Python
[Cloud9] Try to build an environment with django 1.11 of Python 3.4 without understanding even 1 mm
How to deploy a web app made with Flask to Heroku
How to build a python2.7 series development environment with Vagrant
Web application with Python3.3.1 + Bottle (1) --Change template engine to jinja2
Build python virtual environment with virtualenv
[AWS] Flask application deployment version that tried to build a Python environment with eb [Elastic Beanstalk]
Build Mysql + Python environment with docker
Learning history to participate in team application development with Python ~ Build Docker / Django / Nginx / MariaDB environment ~
I tried to make a todo application using bottle with python
Upload file to GCP's Cloud Storage (GCS) ~ Load with local Python
How to build Python and Jupyter execution environment with VS Code
[AWS] Flask application deployment preparation edition that tried to build a Python environment with eb [Elastic Beanstalk]
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Easy construction]
I tried to discriminate a 6-digit number with a number discrimination application made with python
Environment maintenance made with Docker (I want to post-process GrADS in Python
I tried to build a Mac Python development environment with pythonz + direnv
How to deal with old Python versions in Cloud9 made by others
Competitive programming with python Local environment settings
Change Python 64bit environment to 32bit environment with Anaconda
Method to build Python environment in Xcode 6
I want to build a Python environment
Build Python environment with Anaconda on Mac
[Python] A quick web application with Bottle!
Launch environment with LineBot + Heroku + Docker + Python
Build a python virtual environment with pyenv
Build a Python + OpenCV environment on Cloud9
Build a modern Python environment with Neovim
Build AI / machine learning environment with Python
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Trial and error]
Build a local development environment with WSL + Docker Desktop for Windows + docker-lambda + Python
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
How to upload with Heroku, Flask, Python, Git (4)
I made a GUI application with Python + PyQt5
Build python environment with pyenv on EC2 (ubuntu)
Build Python development environment with Visual Studio Code
Build a python environment with ansible on centos6
[Python] Build a Django development environment with Docker
Create a python3 build environment with Sublime Text3
Explaining how to make LINE BOT in the world's easiest way (2) [Preparing a bot application in a local environment with Django in Python]
Build a Python environment with OSX El capitan
Quickly build a Python Django environment with IntelliJ
[Introduction to Udemy Python3 + Application] 9. First, print with print
From Python environment construction to virtual environment construction with anaconda
Build PyPy and Python execution environment with Docker
Build a Python machine learning environment with a container
Build a python execution environment with VS Code
python: 3.8-Handling Exception: you need a C compiler to build uWSGI error with alpine
[AWS] Development environment version that tried to build a Python environment with eb [Elastic Beanstalk]
I tried to build an environment for machine learning with Python (Mac OS X)
[GCP] [Python] Deploy API serverless with Google Cloud Functions!
Build a python virtual environment with virtualenv and virtualenvwrapper
How to deploy a Django application on Alibaba Cloud
How to upload with Heroku, Flask, Python, Git (Part 3)
Build a python environment for each directory with pyenv-virtualenv
I made a Python3 environment on Ubuntu with direnv.
How to build a Django (python) environment on docker
I tried to build ML Pipeline with Cloud Composer