Skip to content

Add flask-smorest dependencies and test route#11

Merged
nbarnabee merged 15 commits intomainfrom
add_smorest_and_routes
Feb 7, 2023
Merged

Add flask-smorest dependencies and test route#11
nbarnabee merged 15 commits intomainfrom
add_smorest_and_routes

Conversation

@nbarnabee
Copy link
Collaborator

This commit squashes a number of smaller commits and adds the following:
flask-smorest as a dependency
A "user" model, schema, blueprint and routes for testing purposes
API documentation (auto-generated by Swagger)

To test: follow the instructions in the README to get everything running and start the db
API docs available at localhost:5000/api/documentation

To test the current route, using Postman (or equivalent), make a POST request to localhost:5000/users with the body

{
  "name":  "WhateverYouWantHere"
}

Then a GET request to the same endpoint should return the list of users.

This commit squashes a number of smaller commits and adds the following:
flask-smorest as a dependency
A "user" model, schema, blueprint and routes for testing purposes
API documentation (auto-generated by Swagger)
@nbarnabee
Copy link
Collaborator Author

Oh, shoot, I should have opened this as a draft request. Feel free to check it out but I don't really want to merge until I've replaced the user test stuff with some real code.

@blancadesal
Copy link
Member

Oh, shoot, I should have opened this as a draft request. Feel free to check it out but I don't really want to merge until I've replaced the user test stuff with some real code.

If this is a WIP and you want to convert it to a draft, you can still do so. There's an option for that in the right sidebar, just below the Reviewers section.

@nbarnabee nbarnabee marked this pull request as draft January 26, 2023 09:43
Renamed "annotations-field" to "field"
Updated models
Introduced schemas for Field, Tool, Task
Wrote and registered /task blueprint
@nbarnabee
Copy link
Collaborator Author

nbarnabee commented Jan 31, 2023

Ok, I've now established a working route at /api/tasks and some mock data.

It does require a small amount of setup:

  • From the command line, docker-compose exec flask-web python manage.py create_db
  • Open a browser window to localhost:5000 You should receive the message "Data inserted!"
  • Go to localhost:5000/api/documentation and run the test to see the structure and contents of the returned data.

@nbarnabee nbarnabee marked this pull request as ready for review January 31, 2023 07:52
@nbarnabee nbarnabee requested a review from Damilare1 January 31, 2023 07:52
@nbarnabee
Copy link
Collaborator Author

Ok, I'm done with updates on this branch (aside from requested revisions, of course!)

It's gone well beyond its original remit, but we now have a working endpoint at api/tasks that properly distinguishes between completed and incomplete tasks. I've also upgraded SQLAlchemy to V2.

Copy link
Collaborator

@HWaruguru HWaruguru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. only thing is that you need to expose the database port in docker-compose so that it can be easy to view the tables in a tool like Tableplus

@nbarnabee
Copy link
Collaborator Author

Looks good. only thing is that you need to expose the database port in docker-compose so that it can be easy to view the tables in a tool like Tableplus

Can you open a pull request for that? I don't use any tools like that and am not sure how to configure them. If it's just a matter of exposing a port it should be an easy fix, I guess. (But nothing seems to happen easily between me and Docker.)

@nbarnabee nbarnabee mentioned this pull request Feb 1, 2023
@@ -0,0 +1,2 @@
from flask_sqlalchemy import SQLAlchemy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what reason(s) did you move the initialization of the db to its own file from /api/__init__py?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC once I started importing it into various locations I started running into error messages; I may have gotten myself stuck in some sort of circular import loop. I'll test it out and see if I can pin down the problem.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. I was just caught up in the idea that I had to do all of my imports first. I can solve the problem by initializing the db and then importing the blueprints, etc. Will push a fix.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data/__init__.py Outdated
from api.models import Tool, Field, Task
from api.resources.db import db

def insertData():
Copy link
Member

@blancadesal blancadesal Feb 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be moved to manage.py so the db could be seeded from the command line (or a script). That way, we could also remove the call to insertData from inside our index route in app.py

nit: Python variables are conventionally named using snake_case. I know you know this, but JS has a way of polluting the mind xd

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved with 3889f2d

@@ -0,0 +1,11 @@
fieldData = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having all mock data inside a single file would reduce complexity a bit

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved with 3889f2d

@blancadesal
Copy link
Member

blancadesal commented Feb 1, 2023

There's a lot going on in this PR, which is mainly my fault for not getting to it earlier. Let's do a little bit of cleanup step by step. We can start with the db stuff.

We already have the flask-migrate plugin – we can remove create_db from manage.py and instead initialize the db with poetry run flask db init. This would create a migrations folder that needs to be committed. I think there's some guidance in the flask-blueprint README.

I've also left some comments inline.

@nbarnabee
Copy link
Collaborator Author

There's a lot going on in this PR, which is mainly my fault for not getting to it earlier. Let's do a little bit of cleanup step by step. We can start with the db stuff.

We already have the flask-migrate plugin – we can remove create_db from manage.py and instead initialize the db with poetry run flask db init. This would create a migrations folder that needs to be committed. I think there's some guidance in the flask-blueprint README.

I've also left some comments inline.

I'd been slacking on properly implementing migrations. I've addressed the other points and will get to this one next.

@nbarnabee nbarnabee requested a review from blancadesal February 2, 2023 06:02
@nbarnabee
Copy link
Collaborator Author

So as of af7f574 I'm up to the following commands for initializing and inserting data into the db:

From the flask container, flask db upgrade which will apply the current migration and build the tables.
From the command line, docker-compose exec flask-web python manage.py insert_data

Is there a way to automate these processes? I tried various ways of inserting these commands into the Dockerfile and docker-compose, but without success.

@blancadesal
Copy link
Member

Is there a way to automate these processes? I tried various ways of inserting these commands into the Dockerfile and docker-compose, but without success.

Yes – this is what the start script we have hanging around inside compose/flask can be used for. To make it work, we'd need to:

  • Edit the Dockerfile to copy over the start script from our directory to inside the container. To avoid some issues on Windows, we should add the line RUN sed -i 's/\r$//g' /entrypoint. The script also needs to be given execution permissions.
  • Edit docker-compose.yml so that it runs the start script instead of whatever command it's running right now
  • Edit the start script to run whatever commands we want at start-up

flask run --host=0.0.0.0 --port=5000
#flask db upgrade
flask run --host=0.0.0.0 --port=5000 No newline at end of file
#python manage.py insert_data No newline at end of file
Copy link
Collaborator Author

@nbarnabee nbarnabee Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recalled the cv-model-serve code you'd shown me before and copied a lot of that. I was able to get things running using /start but the other commands here in the start file had no effect. (They're just commented out here because they weren't doing anything and I wanted to commit a "working" version.)

(I was intending to highlight multiple lines of code here but obviously that didn't work out. I'm referring to the contents of compose/flask/start)

@nbarnabee nbarnabee mentioned this pull request Feb 2, 2023
* Add /api/contribution endpoint

* Add route for getting leaderboard data
@nbarnabee nbarnabee merged commit 24b89c4 into main Feb 7, 2023
@nbarnabee nbarnabee deleted the add_smorest_and_routes branch February 7, 2023 11:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants