My wife is a sports coach. She does this at multiple levels, including personal training.
What that means is the need to manage training sessions. So, smart-guy here said "I could probably build you a simple booking app".
Well, if I'd been listening harder I probably would have heard the sound of all the things I didn't know about web development about to fall on me.
So, let's start at the start. HTML, CSS, and Javascript. Good, good, and meh. But JS is simple enough that me and Stackoverflow can muddle by. So, I think, that's the client side. So what about the backend?
Well, I think, I know Python. What about that?
I looked at Django and Flask and decided that it's relative simplicity meant Flask was the framework for me.
Well, it turned out that it does work for me and the way I think, but suddenly I'm finding that the client/backend split is not clean in the world of Flask.
Two weeks later and I have several functions that render the pages, other functions that read and write from/to a CouchDB, functions that build forms (WTForms module), and a whole bunch of jquery (which I had to learn wholesale because I wanted to use a jquery module called FullCalendar).
The site is looking good, but I learned a lot of things that I intend to write up properly to help other newbies. In the absence of that good write-up, here's a few pointers:
What that means is the need to manage training sessions. So, smart-guy here said "I could probably build you a simple booking app".
Well, if I'd been listening harder I probably would have heard the sound of all the things I didn't know about web development about to fall on me.
So, let's start at the start. HTML, CSS, and Javascript. Good, good, and meh. But JS is simple enough that me and Stackoverflow can muddle by. So, I think, that's the client side. So what about the backend?
Well, I think, I know Python. What about that?
I looked at Django and Flask and decided that it's relative simplicity meant Flask was the framework for me.
Well, it turned out that it does work for me and the way I think, but suddenly I'm finding that the client/backend split is not clean in the world of Flask.
Two weeks later and I have several functions that render the pages, other functions that read and write from/to a CouchDB, functions that build forms (WTForms module), and a whole bunch of jquery (which I had to learn wholesale because I wanted to use a jquery module called FullCalendar).
The site is looking good, but I learned a lot of things that I intend to write up properly to help other newbies. In the absence of that good write-up, here's a few pointers:
- Everything in Flask that is initiated from the client (i.e. the browser) is some kind of call to a URL. Everything.
- For example, your home page "/" might have a button that retrieves data from a database.
- When you click the button, you should be making a call to a URL (e.g. "/getData")
- In Flask, you would have a function under @app.route('/getData')
- The above "@" is a decorator. It is worth spending time on Google to understand python decorators - they are powerful and, in Flask, mandatory.
- The function called would not render a page, but could make the call to the database and package the information before returning it to the client.
- Your page is then passed the data from the function call
- You might be tempted to use the Python couch module if you use CouchDB. Don't. Flask has an extension module that works better, especially because it can use the Flask app's config file to store the database variables.
- You can return data as parameters or as json (using the jsonify function)
- Parameters are appended to the end of the URL (e.g. /getData?name=john) so they are visible to the end user
- JSON is passed without being added to the URL but requires some kind of handling to use it.
- Jquery (or any javascript) uses JSON as standard, so a function to handle the data is likely to be simpler to create.
- Flask does not inherently support YAML configuration files. If you want to use them then this Gist I forked will work. If you don't understand what is happening, read up on Class inheritance in Python. The Gist works by taking in the base Flask Classes that control config management and extending them to support YAML. I added some minor updates to the code to make it work correctly in my Python3 environment.
Comments
Post a Comment