Monday, October 30, 2017

Getting Started In Flask

Flask

For this post this week I will be creating a simple app using Flask that will allow users to post comments, it will also be password protected so it doesn't get full with spam.  Please go to jpernick.pythonanywhere.com and comment to see it in action!  

Getting Started

What are some benefits of using Flask?  Flask is relatively new, only in use since 2010.  It is considered more python-like because the code is more explicit in most cases.  Flask is great for beginners because it is very simply to get a simple app on the web. The Flask app is created the on pythonanywhere.com the same way the Django app was started, by simply going to the files tab and clicking create a new site. This creates a mysite directory and a file called flask_app.py which contains the simple code of the website: *Please click on pictures to enlarge them
and after saving this and going to jpernick.pythonanywhere.com we can see this:

Template 

The next thing I did was design a template for the main page.  I did this by clicking in the create new directory text box and creating a directory called templates.  I then created the file main_page.html.  The file tree now looks like this:
Next I updated the main_page.html file to look like this: 
The next step is to update the original source code to render this template, I changed the flask_app.py code from the original:

to this:

After saving the app this is what I get:
If I try to add this comment though, I get this error:
which actually makes perfect sense because we did not write any code to handle this.  So I did that next.  What this error means is that I tried to send data to the page, but it is not configured to accept it.  The first thing I did was edit the code in flask_app.py from:
to this:
After this change, now I won't get an error when I try to post it, but it will just return the same page and forget about the comment. What I need to do next is connect to a database.  

Database

In order to create a database I went to the databases tab and entered in a password that I will use when I want to access it, and then clicked the initialize button.  The tab looks like this.  


Now I am given the option to create a database, I typed in comments and clicked create.


Next week I will show how to make changes using the database.  Thank you for reading! Please comment anything you would like to see.  

-Joe




Monday, October 23, 2017

Testing the Website

Testing 

In my last post I created a polls application that asked a question, allowed the user to vote, and displayed the results on separate pages. For anyone who missed it, the question was "which platform would you like to see next?" and Flask won! *Please click the photos to enlarge them
Although, I think many of the votes may have been my friends randomly voting to help me test it so if you would like to see one of the others please leave a comment and that's what I will do for the next platform!  For this post, I will show how to test the website.

Automated Testing

Tests are simple routines that test the operation of your code.  Why would I need tests?  A good reason is that it will save you time in the long run, especially for complex websites with multiple applications and interactions where simply "checking to see if it works" is not enough. If something is wrong, tests will also help you find out where the error is.   Some people follow a coding discipline called "test-driven development" in which the tests are written before the code, but it is never too late to get started. 

There is already a bug in the polls application.  The Question.was_published_recently() returns True if the question was published within the last day but also if the questions pub_date is in the future(which is incorrect).  I've used the shell to show this here:

Creating a test to catch the bug

A conventional place to put tests is the applications tests.py file.  So for this site I have updating the polls/tests.py page like so:
What I did was create a django.test.TestCase subclass with a method that creates a Question instance with pub_date in the future. Then it checks the output of was_recently_published() which should be false. When I ran python manage.py test polls the output is:
This shows one failure. The error is easily fixed by updating the method in the polls/models.py file:

After running the test again you can see the error has been fixed and the test passes: 


Making more comprehensive tests

It would be bad if fixing one error created another error so I made some updates to the polls/tests.py file to include two more tests:
This will cover any other issues with the was_published_recently() method.  

Thank you for reading! For my next post I will cover some of the different ways to style a Django page.  Please comment anything you would like to see or any suggestions you would like to give to make your reading experience better. 

-Joe




Monday, October 16, 2017

Django Poll App

Polls Application


This week I created a polls application in Django.  It uses a data base to store questions and choices and then displays the results on a separate page.  I have also created an admin page in which I can access the database and make changes.  Please go to jpernick.pythonanywhere.com/polls to see it in action and vote on the next platform you'd like to see me use.*Please click on the photos to enlarge them. 

Creating the polls directory

I created the polls directory the same way I created the hello app in the last blog post, with the command 'python manage.py startapp polls'.  This is what we created:
For this app, I created two models, a question and a choice.  Each choice is associated with a question.  These are located in the polls/models.py file:
Next I ran a few commands into a bash console. By running 'python manage.py makemigrations polls' it creates migrations for the changes made to models.py.  Next I ran 'python manage.py migrate' to apply those changes to the database.

Updating the database and creating the admin page

There are two different ways to update the database.  One way is by using the command 'python manage.py shell'  This opens up the python interactive shell:
You can run a number of commands in this shell that will manipulate and display data.
.objects.all() - will show all of the objects
.objects.filter(id=1) - will filter results to show the question with id =1
q.choice_set.create(choice_text = "example", votes = 0) - will create a choice related to the question

Another way to update the database is through creating an admin page. First I ran the command 'python manage.py createsuperuser' and was prompted to give my desired username and password.  I can now reach the admin page for the site at jpernick.pythonanywhere.com/admin and after logging in I can see this page:
Now I can click on the change button and make changes to the database:
In order to make the question data accesible on this page I updated the polls/admin.py page to look like this:
from django.contrib import admin
from .models import Question
admin.site.register(Question)

Views, urls, templates

I made three templates to hold the html code for each of the web pages.  But before I updated these I had to update the polls/views.py file:
This code connects the views to each of the templates.  It also contains what to do when someone votes in the poll or if they try to submit a vote without selecting a choice.  The polls/urls.py contains the code to make a call to the view to show in the browser:
Next I updated each of the templates found in mysite/polls/templates/polls/ :
index.html

details.html

results.html




Thank you for reading!! Please comment what you would like to see next and don't forget to go to jpernick.pythonanywhere.com/polls to vote in the poll!

-Joe






Sunday, October 8, 2017

Creating a Simple Web App

Create the app 

The first thing I did was go into the web tab and create a new web app.  This creates a directory in which I can create and edit the files making up the website. *Please click the photos to enlarge them :)





This is the mysite directory.  








Creating "hello"

In order to create the hello directory that will be the hello page of this site I typed this command:
into a Bash console which created these files:


Connect the view

In order to link the view to the site there are a few things that need to be done.  First I updated the hello/views.py file to look like this:
I then had to create a new urls.py file for the hello directory, this is done by just creating a new file and updating it like so:

creating the file






hello/urls.py










Next I had to link this to the mysite/urls.py file:

Lets see if it worked!

Running the command python manage.py runserver 8002 starts a server with the port number 8002.  

Now if I go to my domain name and specifically the hello page at jpernick.pythonanywhere.com/hello we can see that it worked!  The page is displayed with the message.  

Thank you for reading! Next week I will be creating a polling application using a couple cool features in Django.  Please comment your thoughts and anything you would like to see in future blogs!

Tuesday, October 3, 2017

Getting Started

Welcome to my blog! 

      Hello new readers, coding in the cloud is becoming more and more popular.  For this blog I will be creating a website, show how to use python in the cloud and discover the many features of cloud computing using the site python anywhere.  Let's get started!

Console

Python Anywhere allows you to open a console and start coding right away.  The site recommended that I use Python 3.5.  The console tab:   *PLEASE CLICK ON THE PICTURES TO ENLARGE THEM :)


By clicking on python 3.5 a new console opens up.  Here's a quick example to see how it works:


Creating a Web App

The web tab lets you create a new web app using a few different platforms.  Today I chose to use Django, but I will be using each and seeing the differences(if any) later in the blog.  The web tab displays all the info about your application including the traffic its receiving:


I know have a Django web application hosted on the cloud! The url for this site is jpernick.pythonanywhere.com, but you can choose any domain name you want. It gives you a nice welcome message:


That's all for today, thank you for reading! Please comment with any topics you would like to read about in the future!

-Joe