r/flask•
O2_AC

My first simple recipe Flask/Postgres app.

I started learning some coding by enrolling in the CS50 class from Harvard on edX and I am now hearing their Web Development class. Long story short, I built an ugly recipe app. The aim for me was to built the whole functionality (logic) and not focus on web design at the moment. The project is on github: www.github.com/O2-AC/recipes

It uses Flask as Backend and Postgres as database. Feel free to try yourself. First run create.py to set up your database (DATABASE_URL must be given as an environment variable). After setting up the database simply run the Flask application.

I would like to get some feedback. It's the first proper app that I build. Currently it is not possible to edit or delete a recipe, but this will come very soon :)

Edit: Check it out running here: http://recipe50.herokuapp.com

Posted Mar 10, 2018, 8:23 PM UTC · edited

149
boxideaAdvanced•

In your "view all receipes" route, is there a reason you're building a list the way you are?

https://github.com/O2-AC/recipes/blob/e05ef28d80300aaa31c4211b67db51605ee27b08/application.py#L108-L115

I suspect you could just do db.session.query(Recipe) to get a list of all the recipes, since that seems like what you're trying to do.

On this line, you seem to be referencing ingredient variable, but that variable is not defined.

https://github.com/O2-AC/recipes/blob/master/application.py#L53

Also the fact that you're doing db.session.expire in an "add recipe" view tips me off that there might be a better way to do what you're trying to do. You probably don't need to expire the recipe. Just find it by name like you're doing, then update it if it already exists.

2
O2_ACOP•

Thank you so much for the feedback. Regarding your first point, you are absolutely right. I just didn't realize/know that you could get all your items from a table with that code. Regarding the expire(). I think at one point I had some code before this line where I was already adding recipe to the database, but not comitting/flushing. Since I didn't know if this change would persist until the current user would commit something, I thought it would be safer to just get rid of that object. To be honest, I have no idea if that is what "expire()" would achieve or not. But I will definitely use your approach there. Although I won't run the update function here, since this would directly overwrite an existing recipe, which I don't want. I'm trying to write some code at the moment that lets the user edit an existing recipe, which is what I think would be a better solution.

1
deadpit•
1
Apth•

As someone trying their best to write nice, readable code - could you give one example from OP's code that could be changed?

2
bpeller•

This is not very tasty

As far as PEP8:

  • lines are longer than 80 chars
  • camelCasedVariablesAreBothUnPythonic_and_less_readable_than_snake_case
  • there are a whopping 2 blank lines separating at least 5 or 6 different logical sections (each of which should all be refactored out of that view function, but that's not really PEP8-specific)

Regarding the refactoring:

View functions are only supposed to deal with things that deal directly with the request and response. Querying recipes, creating recipes, processing ingredients, none of that stuff belongs in a view function. I would refactor it to have use RecipeManager and IngredientManager classes that have methods like

  • 'get_by_name` (self explanatory)
  • create(name, description) (calls Recipe(**kwargs), adds it to the session and returns the instance)
  • add_to_recipe
  • i gave up trying to decipher the rest of what that function is doing, but you get the idea
3
Apth•

Genuinely helpful, thanks!

2
O2_ACOP•

Thanks for the advice, I'll consider the refactoring.

1
Baseball_Fan•

Do you have a link to the web development course you are taking?

1
O2_ACOP•

Sure it is the CS50 Web Development course. https://cs50.github.io/web/

1