7.6 Get more context: databases and deployment - Video Tutorials & Practice Problems
Video duration:
17m
Play a video:
<v ->Okay, so now that we have a web app,</v> what are some next steps in order to get it to a more full app and also get it onto the internet? Well, the main thing is for our data storage, we were just using global variables in Python, but that's not a very good idea, you know. If you want to make sure your data stays safe and it doesn't, if you restart the server it doesn't change all of your data back to initialized data, you're gonna need to store that data in something like a database. So, a simple data storage solution would just be to write to a file that gets stored on your computer. But there are also more advanced databases that are queryable instead of just reading from a file, you can actually query the content that's in it, and it can store various amounts and make sure the data is a certain format before it gets inserted. So databases are useful for storing data for future use. You can also create backups of data so you can save, take like a snapshot at a given point in time and rollback if you need to. It can also help you with limiting what different users can access. So if you've got a user model and that user can only see certain things, that database is gonna help you not just show everyone everything. And then also, you can encrypt data if you wanted to, it makes data access and storage saving a little bit slower, but if you have sensitive information like passwords, you can transform them, save them, and then when you retrieve it, transform it back before showing it to the user. So there are a number of ways of interacting with these database tables, and the first one is the most basic one and it's using SQL, which is a Structured Query Language, so it's a set way of querying into your data, or querying your database to get the data that you want. And you can learn about it a W3Schools as well. There are also query builders, which provide shortcuts for writing SQL queries and it allows you to write these queries but in your language that you're doing the rest of your work in. So, in Flask and in Python we can write Python code that would then get translated into SQL, and we'll see what this all looks like in the next slide. Lastly, there are ORMs, or Object Relational Mappings, and it allows you to query your data based on objects, like a Python object, as opposed to tables, like columns and rows, and we'll also see that in the next slide. So, in this first example, this is what SQL would look like and it would just be text that has SELECT, and you select which columns you want and then FROM is which table you want. So, for example, it could be if you thought of it as a bunch of Excel spreadsheets, it would be like which spreadsheet view that you wanna get from and which columns. And then where would say which rows do you want? Do I just want one row, the one that has an ID of 10, or do you want everything in the table? So this is the SQL language format at its most basic. There are a lot more complicated things you can do to queries. And then the next one are query builders, so you can see that it's simplifying or creating shortcuts for building SQL queries and I can write it in Python if I'm using a query builder for the Python language, so I can say from the table for persons or people, where the ID is 10, give me the first person. And then from there, I've got an object and I can say person.first_name to get the first name of what was returned to me. And then Object Relational Mapping goes one step further and then treats these tables as actually object classes. I can use the person's class, which gets mapped in the back end to a table in my database, and say get id equal 10, and then from there, from that person, get their first name. And this is like what the Django ORM that comes built in with Django looks like, whereas the query builder is more what it would look like if you're using SQLAlchemy and using it with Flask. So in terms of different databases, like that was how to query a database, but you can also have different databases themselves. There's one called SQLite, and it's actually just a text file and it's a text file that sits on your computer and it's great for making quick databases and development, but you shouldn't use it for production, but it's a good first step. And it allows you to store data, create tables, and query for data from them using SQL or using a query builder or ORM. There's also Postgres and MySQL, these are both good options for production environments, and they're open source and yeah, each of these have slightly different languages for accessing the data, but they're all based on SQL. And then there's also NoSQL databases, there are a number of types of them, like MariaDB, MongoDB, Redis, and these are ones that are not based on columns and rows, these are more like loose structure and it depends on the NoSQL database, but their storing things called documents, so we're not covering this, it is more advanced, but it's an interesting topic that you can lookup at some point in the future. So in Flask, as I mentioned earlier, the common ORM, or query builder library that is used is SQLAlchemy, and it can be used with SQLite, Postgres, or MySQL, but not with NoSQL databases. It allows you to write Python classes that describe the data models, you can create migration files that will allow you to edit the database schema, so that's editing what the tables look like, you can add columns, delete columns, create relationships between tables, and then it allows you to run those migration files that actually change the schema in your database and then you use those Python classes to get an update data. So that's all I'll be talking about for databases, there are a number of tutorials and documentation about using SQLAlchemy or other ORM and query builder options. When we say the word deployment, that means actually getting your website onto the internet. So, if you imagine your website is here, this is your back end and this is your front end, and it's currently not accessible, it has an address, a local host, but nobody else can actually get there. So how do you let people around the world get access to your website? So one is you can make your computer accessible on the internet, you can configure your router that your computer is connected to, and say if someone goes to your public IP address then what gets served to them is your website. But don't do this. (laughs) This is something that's possible, but it's not a good solution. So another option you could do is moving the code to a computer that is accessible on the internet already, maybe one that's managed by a company who can do all that networking for you. I mean, these two options were more common for companies back in the day, where there would some dedicated computers and the people working there would allow the internet to access them, and they would manage those servers or computers. For you and for our purposes, and even for a lot of businesses nowadays, we actually give our code to a service that can then put it on multiple computers. So, why would we do this? Okay. I'm gonna redo this, I forgot I had all these little diagrams. (laughs) Okay. Or, actually, maybe we can just insert them. So this is the one for this slide, and we'll have to put the slides and the audio together. Yup. So, why would we do this? So this is pretty much what cloud hosting is and you would give your code to a service like Amazon Web Services, or Heroku or Microsoft Azure <v Man>Can you say that again?</v> <v ->But don't do this. (laughs)</v> This is something that's possible, but it's not a good solution. So another option you could do is moving the code to a computer that is accessible on the internet already, maybe one that's managed by a company who can do all that networking for you. These two options were more common for companies back in the day where there would be some dedicated computers and the people working there would allow the internet to access them and they would manage those servers or computers. For you and for our purposes, and even for a lot business nowadays, we actually give our code to a service that can then put it on multiple computers. This is pretty much what cloud hosting is. You don't send your code, but they give you access to a computer that you can upload your code to, and then they're able to scale your website and put it on multiple computers if they wanted to, or just have it on one. That's useful because, first of all, you don't have to handle those computers. These computers or servers, these servers don't have a display or anything, they're just really stripped down computers. These servers will live somewhere, someone will manage their maintenance and upgrade the operating system maybe, they'll have a bunch of fans to cool them down, and importantly, they can put your code on multiple computers if you ask them to, or you can have it automatic where, if you have enough traffic, they'll just scale it and copy it onto multiple computers. So the reason that that's helpful is if your code for your website is only on one computer, it can only handle one request at a time. So, if say 10 users make a request to your website, it has to handle each on individually and another one won't get handled until the previous one was finished. So, the more computers that your code is on, the more requests that can be handled at the same time. These computers can all access the same database so that it's consistent across computers. So, there are a few different options for deploying to cloud services. The ones that I would recommend for beginners, and especially with Python, are PythonAnywhere and Heroku. They both have some reasonable free options where you can get your code on one computer. It might not be available 24/7, or it might limit how much time people can spend on it, but it's great for just hobby programmers and having your stuff out there and being able to show it to people, and even just getting experience going through this process is great. So they'll have tutorials about how to get a Python project with Flask or Django onto their servers. Other options are AWS with Amazon, Azure with Microsoft, Firebase and other Google services, these are all very good options for more established businesses that are gonna be scaling and using quite a few resources and these services are all paid by the month, pretty much. There are also lots more options. So, in terms of other considerations for web apps, you could think about authentication, having users stored with logins and passwords, admin views for having a view that staff can manage data without directly writing SQL and editing in the database directly. You can also do form validation, look into that. You can do both back end validation to make sure that invalid data doesn't get saved to your database, and also front end validation in java script, so that the user knows whether they're putting in something that's valid or not. So, for example, if you fill out a password and you don't get information back until after you try to send it that that password needs to have more letters or something or characters, that's because they only have back end validation and not front end validation. And then also look at mobile, that would be more of a front end CSS kind of thing, but handling mobile views is more and more important these days because more people are accessing websites through their phones rather than their desktop or laptop computers.