[Python + heroku] From the state without Python to displaying something on heroku (Part 2)

What is this?

This is a continuation

[Python + heroku] From the state without Python to displaying something on heroku (Part 1) http://qiita.com/it_ks/items/afd1bdb792d41d0e1145

So far I deployed it on heroku and ran it as a sample <Imakoko

heroku update I forgot. It's a classic, isn't it? heroku update

>heroku update
Updating plugins... done. Updated 1 package.
heroku-cli: Updating... done.

It seems that heroku-cli was updated at this time, and it was updated.

virtualenv

to make

virtualenv venv-name It builds the immediate environment settings to switch and use. Venv-name is the environment name. Give it a name of your choice and run it. Here, we will make it orthodox with the name "venv".

>virtualenv venv
New python executable in venv\Scripts\python.exe
Installing setuptools, pip, wheel...done.

Then, now C: \ Users \ (...) \ python-getting-started I'm in, but a folder called venv is created directly under it. There are various Pythons (about 40MB) in it, This is a set of environments that can be polluted.

To enable.

There is an activate.bat, so use it. Then, the environment name will be added to the prompt (at the beginning of waiting for command line input).

python


>venv\Scripts\activate.bat
(venv) C:\Users\(Omitted)\python-getting-started>

Hereafter, it will be abbreviated as (venv)>

Push local changes Imakoko

https://devcenter.heroku.com/articles/getting-started-with-python#push-local-changes

Install from requirements file

python


(venv) >pip install -r requirements.txt
Collecting dj-database-url==0.3.0 (from -r requirements.txt (line 1))
  Downloading dj_database_url-0.3.0-py2.py3-none-any.whl
Collecting Django==1.9.1 (from -r requirements.txt (line 2))
  Downloading Django-1.9.1-py2.py3-none-any.whl (6.6MB)
    100% |################################| 6.6MB 75kB/s
Collecting gunicorn==19.4.5 (from -r requirements.txt (line 3))
  Downloading gunicorn-19.4.5-py2.py3-none-any.whl (112kB)
    100% |################################| 114kB 1.7MB/s
Collecting psycopg2==2.6.1 (from -r requirements.txt (line 4))
  Downloading psycopg2-2.6.1-cp27-none-win32.whl (814kB)
    100% |################################| 815kB 487kB/s
Collecting whitenoise==2.0.6 (from -r requirements.txt (line 5))
  Downloading whitenoise-2.0.6-py2.py3-none-any.whl
Installing collected packages: dj-database-url, Django, gunicorn, psycopg2, whitenoise
Successfully installed Django-1.9.1 dj-database-url-0.3.0 gunicorn-19.4.5 psycopg2-2.6.1 whitenoise-2.0.6

(venv) >

Edit requirements.txt

requests==2.9.1 Is added to requirements.txt, so add it After that, pip install -r requirements.txt

python


(venv) >pip install -r requirements.txt
Requirement already satisfied (use --upgrade to upgrade): dj-database-url==0.3.0 in c:\users\cat_high\documents\guncys\herokustudy\proj\testapp\python-getting-started\sgenv\lib\site-packages (from -r requirements.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): Django==1.9.1 in c:\users\cat_high\documents\guncys\herokustudy\proj\testapp\python-getting-started\sgenv\lib\site-packages (from -r requirements.txt (line 2))
Requirement already satisfied (use --upgrade to upgrade): gunicorn==19.4.5 in c:\users\cat_high\documents\guncys\herokustudy\proj\testapp\python-getting-started\sgenv\lib\site-packages (from -r requirements.txt (line 3))
Requirement already satisfied (use --upgrade to upgrade): psycopg2==2.6.1 in c:\users\cat_high\documents\guncys\herokustudy\proj\testapp\python-getting-started\sgenv\lib\site-packages (from -r requirements.txt (line 4))
Requirement already satisfied (use --upgrade to upgrade): whitenoise==2.0.6 in c:\users\cat_high\documents\guncys\herokustudy\proj\testapp\python-getting-started\sgenv\lib\site-packages (from -r requirements.txt (line 5))
Collecting requests==2.9.1 (from -r requirements.txt (line 6))
  Downloading requests-2.9.1-py2.py3-none-any.whl (501kB)
    100% |################################| 503kB 518kB/s
Installing collected packages: requests
Successfully installed requests-2.9.1

(venv) >

Edit hello / views.py

Follow the instructions and add the following to the beginning of views.py import requests

In addition, modify the index function

python


# Create your views here.
def index(request):
    # return HttpResponse('Hello from Python!')
    return render(request, 'index.html')

python


def index(request):
    r = requests.get('http://httpbin.org/status/418')
    print r.text
    return HttpResponse('<pre>' + r.text + '</pre>')

Test locally

After rewriting the index function, test

(venv) >pip install -r requirements.txt
(venv) >heroku local
forego | starting web.1 on port 5000
web.1  | Traceback (most recent call last):

(venv) >

You can check the local environment by accessing http: // localhost: 5000.

Supplement

I noticed after this, but maybe this is the case in the windows environment. heroku local web -f Procfile.windows The one written at the bottom of here is ↓ https://devcenter.heroku.com/articles/getting-started-with-python#declare-app-dependencies

Reflected in the file on heroku

Still here

https://devcenter.heroku.com/articles/getting-started-with-python#push-local-changes

git add

git add .

(Then warning: LF will be replaced by CRLF in〜〜 The file will have its original line endings in your working directory. A lot of logs about line feed code are returned. abridgement)

git commit

(venv) >git commit -m "Demo"

*** Please tell me who you are.

Run

  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got '{user}@{machine}.(none)')

(venv) >

Hmm Excuse me.

(venv) >git config --global user.email "[email protected]"
(venv) >git config --global user.name "heroku user name"

And commit again.

Confirm with git push

It says git push heroku master, so follow it quietly

Uploading takes some time. About 2 minutes?

python


(venv) >git push heroku master
Counting objects: 5952, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4984/4984), done.
Writing objects: 100% (5952/5952), 7.32 MiB | 87.00 KiB/s, done.
Total 5952 (delta 1807), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Using set buildpack heroku/python
remote: -----> Python app detected
remote: -----> Installing dependencies with pip
remote:        Collecting requests==2.9.1 (from -r requirements.txt (line 6))
remote:        Downloading requests-2.9.1-py2.py3-none-any.whl (501kB)
remote:        Installing collected packages: requests
remote:        Successfully installed requests-2.9.1
remote:
remote: -----> Preparing static assets
remote:        Running collectstatic...
remote:        Post-processed 'admin/js/vendor/xregexp/LICENSE-XREGEXP.txt' as 'admin/js/vendor/xregexp/LICENSE-XREGEXP.d64cecf4f157.txt'
(Omitted)
remote:        Post-processed 'humans.txt' as 'humans.d41d8cd98f00.txt'
remote:        Post-processed 'lang-logo.png' as 'lang-logo.019c8743b7cf.png'
remote:        58 static files copied to '/app/staticfiles', 58 post-processed.
remote:
remote:
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing... done, 50.3MB
remote: -----> Launching...
remote:        Released v6
remote:        https://ancient-taiga-0000.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy.... done.
To https://git.heroku.com/ancient-taiga-0000.git
   fe7f948..8bba81f  master -> master

(venv) >

Check with (venv)> heroku open (for heroku that is not local).

console

Imakoko

https://devcenter.heroku.com/articles/getting-started-with-python#start-a-console

Python Try heroku run python manage.py shell

python


(venv) >heroku run python manage.py shell
Running python manage.py shell on ancient-taiga-0000... up, run.8091
Python 2.7.11 (default, Dec  7 2015, 21:16:24)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
>>> dir()
['__builtins__']

You can talk. The response when typing is slow, probably because of communication.

When closing

>>> exit()

bash heroku run bash

python


(venv) >heroku run bash
Running bash on ancient-taiga-0000... up, run.5888
~ $ ls
app.json        hello      Procfile          README.md         runtime.txt
gettingstarted  manage.py  Procfile.windows  requirements.txt  staticfiles
~ $ exit
exit

(venv) >

Edit the displayed contents

This is the production. This is the index function that I modified earlier,

python


    return HttpResponse('<pre>' + r.text + '</pre>')

This part will be the content that will be displayed when you access the index. You just need to give a string to HttpResponse () **.

For example

python


def index(request):
    return HttpResponse( str(dir()) )

Like.

add、commit、push After editing .py, add → commit → push

(venv) >git add .

(venv) >git commit -m "DemoDemo"
[master 986e6d1] DemoDemo
 1 file changed, 4 insertions(+)

(venv) >git push heroku master
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 365 bytes | 0 bytes/s, done.
Total 4 (delta 3), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Using set buildpack heroku/python
remote: -----> Python app detected
remote: -----> Installing dependencies with pip
remote:
remote: -----> Preparing static assets
remote:        Running collectstatic...
remote:        Post-processed 'admin/js/vendor/jquery/jquery.js' as 'admin/js/vendor/jquery/jquery.107fbe9555bf.js'
(Omitted)
remote:        Post-processed 'humans.txt' as 'humans.d41d8cd98f00.txt'
remote:        Post-processed 'lang-logo.png' as 'lang-logo.019c8743b7cf.png'
remote:        58 static files copied to '/app/staticfiles', 58 post-processed.
remote:
remote:
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing... done, 50.3MB
remote: -----> Launching...
remote:        Released v8
remote:        https://ancient-taiga-0000.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/ancient-taiga-0000.git
   8bba81f..986e6d1  master -> master

(venv) >

(Oh, the commit comment looks like shit, but of course you should add a better poem)

result: heroku_dir.png

Add other than index

The ** index ** function is called when you access https://ancient-taiga-0000.herokuapp.com/, Then, for example, to make it accessible like https: // abbreviation / test.

Function addition

Add a return HttpResponse () function similar to index. Here, we have prepared a "test" function.

python


def test(request):
    return HttpResponse( '<strong>test view !!</strong>' )

Edit urls.py

It seems that you should edit ** urls.py ** directly under the getting started folder along with view.py. This on github,

https://github.com/heroku/python-getting-started/blob/master/gettingstarted/urls.py

python


urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'gettingstarted.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^$', hello.views.index, name='index'),
    url(r'^db', hello.views.db, name='db'),
    url(r'^admin/', include(admin.site.urls)),
)

(▲) Because there is such a part

python


    url(r'^test', hello.views.test, name='test'),

I added it like this.

with this heroku_testview.png

You've reached your goal of displaying something, thank you.

Summary

――It was long up and down, but I didn't do much. The latter half is mainly below --Describe the process you want to display in views.py --Register in urls.py --I didn't mention "** env " or " postgrqsl **" in the original documentation. I don't know

It seems that you should prepare an HTML string as if you are using the strong tag in the final test view. Before that, write a process to communicate with an external service, It seems that you can format it and pass it to the HttpResponse function.

So I want to play around with ** external APIs **.

reference

Preparing and deploying Heroku within the free tier (Mac 10 + Rails 4.2 + MySQL 5.6) http://ruby-rails.hatenadiary.com/entry/20150314/1426332751

Recommended Posts

[Python + heroku] From the state without Python to displaying something on heroku (Part 1)
[Python + heroku] From the state without Python to displaying something on heroku (Part 2)
How to deploy the easiest python textbook pybot on Heroku
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
Introduction to Python Hands On Part 1
Update Python on Mac from 2 to 3
Deploy the Django app on Heroku [Part 2]
Deploy the Django app on Heroku [Part 1]
I made something with python that NOW LOADING moves from left to right on the terminal
Connecting from python to MySQL on CentOS 6.4
Do something regularly from heroku to Shotgun
Introduction to Python with Atom (on the way)
[IBM Cloud] I tried to access the Db2 on Cloud table from Cloud Funtions (python)
[Python] How to remove duplicate values from the list
The wall of changing the Django service from Python 2.7 to Python 3
"Cython" tutorial to make Python explosive: Pass the C ++ class object to the class object on the Python side. Part 2
Think about how to program Python on the iPad
From the initial state of CentOS8 to running php python perl ruby with nginx
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Steps to install the latest Python on your Mac
How to upload with Heroku, Flask, Python, Git (Part 3)
Preferences to generate animated GIFs from Python on Mac
I tried python on heroku for the first time
I wanted to use the Python library from MATLAB
From python to running instance on google cloud platform
Find the part that is 575 from Wikipedia in Python
I sent regular emails from sendgrid on heroku, on python
How to upload with Heroku, Flask, Python, Git (Part 1)
The first step to getting Blender available from Python
How to pass arguments when invoking python script from blender on the command line
How to upload with Heroku, Flask, Python, Git (Part 2)
How to enjoy Python on Android !! Programming on the go !!
"Cython" tutorial to make Python explosive: Pass a C ++ class object to a class object on the Python side. Part ①
How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
How to run Python on Windows without polluting the environment as much as possible (Scoop edition)
Control smart light "Yeelight" from Python without using the cloud
[Part 4] Use Deep Learning to forecast the weather from weather images
[Part 1] Use Deep Learning to forecast the weather from weather images
[Hyperledger Iroha] Notes on how to use the Python SDK
Don't lose to Ruby! How to run Python (Django) on Heroku
[Part 2] Use Deep Learning to forecast the weather from weather images
Save images on the web to Drive with Python (Colab)
Program to determine leap year from the Christian era [Python]
Go language to see and remember Part 8 Call GO language from Python
Pass OpenCV data from the original C ++ library to Python
Extract the value closest to a value from a Python list element
Post from Python to Slack
Cheating from PHP to Python
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Update python on Mac to 3.7-> 3.8
Switch from python2.7 to python3.6 (centos7)
Connect to sqlite from python
How to easily create an environment where python code runs on Jupyter without polluting the local environment
About the error I encountered when trying to use Adafruit_DHT from Python on a Raspberry Pi
The story that the version of python 3.7.7 was not adapted to Heroku
The record I was addicted to when putting MeCab on Heroku
How to rebuild python environment from pyenv on Mac environment (El Capitan)
[Python] Try to graph from the image of Ring Fit [OCR]
How to get followers and followers from python using the Mastodon API
Things to note when running Python on EC2 from AWS Lambda