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
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.
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
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) >
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) >
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>')
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
.
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
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.
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).
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) >
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:
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
.
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>' )
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
You've reached your goal of displaying something, thank you.
――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 **.
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