Python CGI Script, HTML, CSS Example

Here is a link to the “Eliza” cgi-script, html, and css files that we discussed today in class.

Recall that the .css and .py files have specific places where they need to be placed. The client .html page asks the browser to look for the .css file to style the look of the page, and the .html page also has a form which triggers the Python cgi script with its action. Make sure that you place the files relative to your own web server, and they should run as discussed. Also, see chapter 13 in the text for a similar example.

Enjoy.

xkcd and Python

Example Python Code

Here is a .zip archive with example code for several interesting Python-related applications (some written by me, others gleaned from different sources as credited in class):

  • Graphical User Interfaces (GUI) with Tkinter and PyQt.
  • PyAudio for sound recording and playing.
  • NumPy and Matplotlib for math and plotting.
  • URL access and Twitter status updates.
  • Graphics with Nodebox (Mac only).

Final Project Options

1) Investigate PyAudio and digital sound in general.  Create a program that generates 5 different waveforms, graphs them, and plays the sound.
2) Investigate graphical user interfaces and either PyQt and/or Tkinter.  Complete a GUI based program.  One example is the telephone database in Chapter 12 with Tkinter — either complete this in PyQt or modify to organize data about a favorite hobby rather than telephone records.
3) Investigate Maya and Python.  One of the best options is to checkout the “Digital Tutors:  Python Scripting in Maya” disc from the library.  View the whole tutorial and create a summary with text and images as you step through the example in Maya and Python.  Complete the application, and modify in some interesting way.
4) Investigate a web-related technology such as the Google App Engine or Django and build a simple app using your tool of choice.
5) Investigate NumPy, SciPy, Matplotlib (and/or Mayavi).  Generate at least three plots or figures based on real-world data or math problems.
6) Investigate Nodebox.  Get at least two examples running to demonstrate and explain.  Create your own generative art or interactive project or significantly modify and enhance one or more of your examples.
7) Investigate PyGame some more, particularly stepping through one or two interesting examples. Significantly expand your car race game from the earlier project, or create a new game using PyGame.
8) Investigate Panda3D and some of the design and ideas behind 3D game engines.  Get at least one interesting Panda3D example running.  Create your own game or modify and enhance your example with your own code and content.
9) Investigate an example use of problem-solving and/or computing related to a field or hobby of interest to you.  Create a program that solves a simple problem related to that.  It may be text or GUI-based and could generate text, plots, figures, etc. to show your solution.
  1. Investigate PyAudio and digital sound in general.  Create a program that generates 5 different waveforms, graphs them, and plays the sound.
  2. Investigate graphical user interfaces and either PyQt and/or Tkinter.  Complete a GUI based program.  One example is the telephone database in Chapter 12 with Tkinter — either complete this in PyQt or modify to organize data about a favorite hobby rather than telephone records.
  3. Investigate Maya and Python.  One of the best options is to checkout the “Digital Tutors:  Python Scripting in Maya” disc from the library.  View the whole tutorial and create a summary with text and images as you step through the example in Maya and Python.  Complete the application, and modify in some interesting way.
  4. Investigate a web-related technology such as the Google App Engine or Django and build a simple app using your tool of choice.
  5. Investigate NumPy, SciPy, Matplotlib (and/or Mayavi).  Generate at least three plots or figures based on real-world data or math problems.
  6. Investigate Nodebox.  Get at least two examples running to demonstrate and explain.  Create your own generative art or interactive project or significantly modify and enhance one or more of your examples.
  7. Investigate PyGame some more, particularly stepping through one or two interesting examples. Significantly expand your car race game from the earlier project, or create a new game using PyGame.
  8. Investigate Panda3D and some of the design and ideas behind 3D game engines.  Get at least one interesting Panda3D example running.  Create your own game or modify and enhance your example with your own code and content.
  9. Investigate an example use of problem-solving and/or computing related to a field or hobby of interest to you.  Create a program that solves a simple problem related to that.  It may be text or GUI-based and could generate text, plots, figures, etc. to show your solution.
General rules:
Investigate background material, concepts, and code.
Write a brief summary of your research and include example code and figures.
Prepare to present your work in a 5-10 minute long presentation.
You may use example code, but you need to add, expand, or modify in some significant manner.  Credit what you use directly with references, and explain what you added or modified.

Due Thur., Dec. 10 at 3 p.m.

Python in Action

Samples from the many current uses of Python:

Test on Thurs., Nov. 12.

Our second test is scheduled for next week. Questions will come from material in chapters 5-11 in the Python text as well as examples from our Project 1.

Programming Paradigms

If you find the slides, helpful, they are now linked under the resources page and also here.

Homework, Due Tues, Nov. 10

Complete the following:

Ch. 5: Problems #2, 3, 6, and 7
Ch. 6: Problems #1, 2, and 3

If code is required, include a problem#.py file in your folder. If discussion is required, include a answer#.txt or single text (or .pdf, .doc. etc.) that contains your answers.

Email a .zip archive of your folder of solutions.

Exploring Python Text: Dictionaries and Files

Make sure to have read through chapter 10 of the text so far. During our discussion of the PyGame Race Game, we covered most of the material in chapters 7, 9, and 10, concerning classes, object-oriented programming, and modules/namespaces. (We’ll talk a bit more about the functional programming ideas from chapter 8 a bit later).

This week, we’ll pick up a few things we skipped from chapters 5 and 6 on dictionaries and files as well as talk about a few other topics to round out our programming and Python introductory material. Beginning next week, we’ll wrap-up chapter 11 and start to look at the application case studies in the remaining chapters of the text.

For extra reading concerning dictionaries, read the chapter 12 in the online text and the section in the official Python tutorial. And one more example. Also, for another view of files, read chapter 10 in the online text; there are also some nice examples in the official Python tutorial.

There will be a second test coming soon, covering material from chapters 5 – 11.

Project #1: Due Tuesday, October 27

Complete the following and submit on or before the due date:

1) Download this sample code package, a Python/PyGame race game example. “raceGame.py” and “raceGame2.py” are examples that we’ll discuss and will help. “raceGameTwoCars.py” is a code sample that you can build on for your project.

2) Modify the game to run using two cars, one using the arrow keys and the other using ‘w’, ’s’, ‘a’, and ‘d’ to maneuver.

3) Also, do the following:

a) Modify the game so that each car is a different color. (Hint: look in the directory).

b) Modify the game so that when a car goes off the left side of the window, it comes back on the right, etc. for all sides of the window.

c) Modify the game so that there are three bumpers for the red car, three bumpers for the orange car, and three bumpers that should not be hit. Each set of these should look different (such as changing color, image, or whatever).

d) Randomly locate the bumpers across the (x,y) of the game background when the game starts.

e) Re-write the game so that the first player has to hit his bumpers in order (even though the order will not be known since they are randomly distributed), the second player has to hit his bumpers in order, and both cars have to avoid the bumpers to avoid (or some penalty will happen — make this up — they lose, they blow-up, they slow-down, whatever you like).

f) Whoever hits all the bumpers belonging to that car in the proper order first wins.

Bonus (Complete any or as many as possible for extra credit):

a) Play a sound whenever a bumper is hit.

b) Play sounds for the cars whenever they accelerate, collide, or whatever seems appropriate.

c) Give the cars capabilities such as oil-slick, guns, whatever…

d) Display scores on screen — makeup whatever scoring mechanism you like.

e) Have the cars explode if they run into each other.

f) Make your own artwork to replace the images of the cars, bumpers, and background.

←Older