This tutorial steps you through the
process of creating a dynamic web application that runs on Google's
servers using App Engine. The sample you'll create can be used as a
template for creating new interactive web applications. This is a
try-it-before-you-really-know-it tutorial-- you'll get a taste of what
can be done and you'll have a lot of questions as to how it all works.
In the upcoming lessons, we'll go over the sample in detail so you can
understand every line of code. This tutorial uses the Linux operating system, not Windows. If you are logged into Windows, restart your computer. 1. Create a folder named 'grades' within your home folder. This is where all the code will go. You can get to your home folder by clicking the icon labeled, for instance, "Wolber's Home". Create the folder grades by selecting File | create folder and entering 'grades' for the name. 2. In the top menu, open Applications | Accessories | KWrite, which is a text editor. Cut and paste the following HTML code into a file and save the file as 'index.html' within the folder 'grades'. The name index.html is important, so name it exactly that. <html> 3. Save the attachments
at the bottom of this page into your 'grades' directory. The easiest
way is to right-click on them and choose 'Save Link As'. Along with
index.html you should now have 3 files in the 'grades' directory.<body> <h2>GPA Calculator</h2> <h3>Enter a string of grades, e.g, ABCAAB</h3> <form action="/on_calculate" method="get"> Grades:<input type="text" name="grades" value={{grades}}> <input type="submit" value="Calculate"></div> </form> Number of Grades: {{gradeCount}} <br/> GPA: {{gpa}} </body> </html> 4. We'll use the Linux command-line terminal to start up a test server. The terminal is similar to the DOS prompt on Windows machines. To open a command-line terminal, choose Applications | System | Terminal from the top menu. A black window should appear. 5. In the terminal window, start the local test server by entering the following: $ dev_appserver grades Note that the $ is the Linux prompt-- don't type it. 6. The development app server you just started can be accessed at a special URL. Open a Firefox browser and enter the following URL: http://localhost:8080 This will 'run' the program you just created by accessing the server you just started. Enter some grades in the text box, then click 'Calculate'. The number of grades you entered should be displayed. 7. If you've made it here, your program 'works' but its really not on the web yet-- your browser is just accessing a 'test' server on your local machine. But its real easy to upload an application to Google.
$ appcfg update grades 8. Now your ready to run your first dynamic web application! Open a Firefox browser and enter the name of your application, followed by appspot.com. For instance: http://GPACalculator.appspot.com If all is well, you should get the same screen you had with the development server. Only this time, your application is really running on the web. 9. Ask another student to enter your URL into their browser. They should now be able to use your grading program. 10. Now it's time to make some changes to the code in order for it to really calculate the GPA, and for you to understand dynamic web pages better. Your first task is to change the program so that it counts the number of A grades in the input string and reports it to the user.
and replace it with code that loops through the input string to count the number of 'A's in the string grades. Replace the 'count=len(grades) with the following: i=0 count=0 while i<len(grades): if grades[i] =='A': count=count+1 i=i+1 As usual, be very careful when you paste this code in so that you get the indentation correct.
$ dev_appserver grades then refreshing http://localhost:8080 in your browser. Enter some grades and click 'Calculate'. Does it now show you the number of A grades? 11. O.K., let's really compute the GPA now. Modify the code you wrote for (9) so that instead of counting the As, you total up the grade points. Remember your old 'total some numbers' code? Here, you'll need to use if and elif statements so that each A gets 4 points, each B gets 3 points, each C gets 2 points, and each D gets 1 point. You should add these points to a variable total, each time in the loop. After the loop, get the grade point average by dividing the total by the number of grades-- len(grades). 12. Just computing the GPA is not enough, now we need to report it.
by putting in a value for the dynamic variable gpa: template_values= {'grades': grades,'gradeCount':count, 'gpa': computedGPA} Note that calculatedGPA should be the name of the variable you used to store the result of your GPA calculation.
$ control-c to stop the test server $ appcfg update grades 15. Test to see that you application works by opening the URL: http://GPACalculator.appspot.com in the browser (replace GPACalculator with your application name). |