Today was Free Friday, and I worked on two projects: an image copier and the rest api.
Firstly, I noticed one of my underclassman friends had left their computer logged in. Whenever any of us forget to log out, someone usually downloads an image and copies it all over their desktop. At the suggestion of my teacher, I decided to automate the process.
This is the final code for the program. It uses the shutil module to copy the image file from the directory it is currently in, copy it into the desktop, and then add one so the file doesn’t override itself. This is then run in a for loop for as long as one chooses.
The second project I worked on was more of the Rest API. After dealing with a spelling mistake for way too long, I added parameters to our resource. We firstly add them in main.py, which would look like this:
from flask import Flask
from flask_restful import Api, Resource
app = Flask(__name__)
api = Api(app)
class HelloWorld(Resource):
def get(self, name, test):
return {"name":name, "test":test}
api.add_resource(HelloWorld, "/helloworld/<string:name>/<int:test>")
if __name__ == "__main__":
app.run(debug=True)
We then also add these parameters to our get request, which will look like this:
import requests
BASE = "http://127.0.0.1:5000/"
response = requests.get(BASE + "/helloworld/michael/51")
print(response.json())
And that was all I completed today.