[PYTHON] Try adding an external module to pepper. For the time being, in requests.

Good evening n0bisuke. I'm taking notes of what I was addicted to in pepper development. (Last time => Note on creating your own Box with Pepper's Python)

For python, the request module is great. I want to use it positively when communicating with http ... However, pip cannot be used with pepper, so let's do it with the following procedure.

By the way, the photo is @ ukk0 who recently joined our team. It has nothing to do with the content of the article lol

Refer to Using Requests for HTTP communication of Pepper.

request module

A module that makes http requests nice. There is ʻurllib2`, but it's so easy that it's incomparable.

$ pip install requests

app.py


import requests

r = requests.get('http://qiita.com/n0bisuke/items/d07b5485f4e767bc6e20.json')
print r.json()["title"]

Get information about Qiita posts. http://qiita.com/n0bisuke/items/d07b5485f4e767bc6e20.json

$ python app.py
Sample of HTTP GET and JSON parsing with python of pepper

I'll call you in two lines. It's easy.

Use with pepper

It seems that pip cannot be used with pepper, and it is common to insert the entire directory orz

Download it as a zip from github page and insert the contents requests folder into the choregraphe side.

Create a lib folder and put the requests folder in it. 

After that, edit Python Script like Last article.

app.py


class MyClass(GeneratedClass):
    def __init__(self):
        GeneratedClass.__init__(self)

    def onLoad(self):
        self.framemanager = ALProxy("ALFrameManager")
        self.folderName = None

    def onUnload(self):
        import sys
        if self.folderName and self.folderName in sys.path:
            sys.path.remove(self.folderName)
        self.folderName = None

    def onInput_onStart(self):
        import sys, os
        self.folderName = os.path.join(self.framemanager.getBehaviorPath(self.behaviorId), "../lib")
        if self.folderName not in sys.path:
            sys.path.append(self.folderName)

    def onInput_onStop(self):
        self.onUnload()
        self.onStopped()

self.framemanager.getBehaviorPath (self.behaviorId)," ../ lib ", etc. are like specifying the path to the lib folder created earlier.

I'm not familiar with python, so I won't mention it anymore.

app.py


class MyClass(GeneratedClass):
    def __init__(self):
        GeneratedClass.__init__(self)

    def onLoad(self):
        self.framemanager = ALProxy("ALFrameManager")
        self.folderName = None

    def onUnload(self):
        import sys
        if self.folderName and self.folderName in sys.path:
            sys.path.remove(self.folderName)
        self.folderName = None

    def onInput_onStart(self):
        import sys, os
        self.folderName = os.path.join(self.framemanager.getBehaviorPath(self.behaviorId), "../lib")
        if self.folderName not in sys.path:
            sys.path.append(self.folderName)
        import requests
        r = requests.get('http://qiita.com/n0bisuke/items/d07b5485f4e767bc6e20.json') #← Addendum
        title = r.json()["title"].encode("utf-8") #← Addendum
        self.logger.info(title) #← Addendum

    def onInput_onStop(self):
        self.onUnload()
        self.onStopped()

Now you can finally ʻimport requests`. The rest is the same as the python code I wrote at the beginning.

If you execute it and the parsed character string is displayed in the dialog of the collegraph, it is successful.

Sample of HTTP GET and JSON parsing with python of pepper

Recommended Posts

Try adding an external module to pepper. For the time being, in requests.
Molecular dynamics simulation to try for the time being
[Introduction to Reinforcement Learning] Reinforcement learning to try moving for the time being
Try using FireBase Cloud Firestore in Python for the time being
Try using LINE Notify for the time being
Try posting to Qiita for the first time
Flow memo to move LOCUST for the time being
Next to Excel, for the time being, jupyter notebook
I will try to summarize the links that seem to be useful for the time being
I want to move selenium for the time being [for mac]
For the time being, try using the docomo chat dialogue API
I want to create a Dockerfile for the time being.
Put the module in the Pepper app
Cython to try in the shortest
For the time being, I want to convert files with ffmpeg !!
I made a function to check if the webhook is received in Lambda for the time being
python3 How to install an external module
Python Master RTA for the time being
[Python] [Machine learning] Beginners without any knowledge try machine learning for the time being
MongoDB for the first time in Python
Let's try Linux for the first time
For the first time in Numpy, I will update it from time to time
I want to use Ubuntu's desktop environment on Android for the time being (Termux version-Japanese input in desktop environment)
Switch the module to be loaded for each execution environment in Python
I want to see a list of WebDAV files in the Requests module
I want to store the result of% time, %% time, etc. in an object (variable)
Try adding fisheye lens distortion to the image
How to use MkDocs for the first time
For the time being, import them into jupyter
Make a histogram for the time being (matplotlib)
Run yolov4 "for the time being" on windows
I played with Floydhub for the time being
Try to calculate RPN in Python (for beginners)
Mock HTTP requests via the requests module in Responses
virtualenv For the time being, this is all!
Write this in the ttl file for the time being for automatic SSH login with Teraterm
Try to make a Python module in C language
Run with CentOS7 + Apache2.4 + Python3.6 for the time being
Register a task in cron for the first time
Write data to KINTONE using the Python requests module
I will install Arch Linux for the time being.
Try to decipher the login data stored in Firefox
[For beginners] I want to explain the number of learning times in an easy-to-understand manner.
How to calculate the sum or average of time series csv data in an instant
I want to use the Ubuntu desktop environment on Android for the time being (Termux version)
I want to use Ubuntu's desktop environment on Android for the time being (UserLAnd version)
An easy way to view the time taken in Python and a smarter way to improve it
Test code to check for broken links in the page
I tried running PIFuHD on Windows for the time being
Summary of stumbling blocks in Django for the first time
How to manipulate the DOM in an iframe with Selenium
[Understand in the shortest time] Python basics for data analysis
I tried to illustrate the time and time in C language
Display the time in Django according to the user's region (UTC)
Try to extract the keywords that are popular in COTOHA
An introduction to the modern socket API to learn in C
A story that didn't work when I tried to log in with the Python requests module
The story of returning to the front line for the first time in 5 years and refactoring Python Django
If you are a beginner in programming, why not make a "game" for the time being? The story