scratch.py

A Python module by Lion Kimbro.

Download: scratch.py

You should need to know a little about using the command line, and perhaps a little Python, before using this.


Documentation

Hello, World!

Here's "Hello, World!" in scratch.

publishing_namespace = None

def helloworld():
    return "Hello, world!"

Save that to a file, and then copy scratch.py into the same directory.

Run scratch.py: python scratch.py

Take your webbrowser to http://localhost:8000/ and you should be greeted with a page listing helloworld as a function. Click on it, and you'll see: "Hello, world!"

Arguments

Add arguments!

publishing_namespace = None

def greetings(gentlemen):
    return "Greetings, %s!" % gentlemen

Now restart the server, and then go back to localhost.

Click on the "greetings" function, and you'll see that this time, you have a form, asking you to type in a value for field "gentlemen."

Enter an entry, submit, and there it is! "Greetings, gentlemen!"

Customizing

There are many things you can do to customize this.

Try these things out:

Note that when you specify field width & height w/ _80 or _40_20 (or whatever,) that you have to change the variable use within the code, too... Real bummer, hunh? I know.

Also, compare:

publishing_namespace = None

def add_strings(a, b):
    return a + b

def add_numbers(int_a, int_b):
    return int_a + int_b

Very different results, no?

Data Storage and Retrieval

Here's another cool thing.

If you prefix a module variable with "pickle_", it will automatically be pickled.

publishing_namespace = None

pickle_pages = {}

def post_save(title, page_40_10):
    global pickle_pages
    pickle_pages[title] = page_40_10

def get_retrieve(title):
    return pickle_pages[title]

Oh, note that the "get" there is just to make explicit: "This works by HTTP GET request."

That code above gives you a super-rudimentary wiki-like system.

Each time a function is used, it will pickle.

Now, that's not very safe-- your pickled database will crash eventually, when two users (or, just you) try to use the website at once.

But this app is called "scratch" for a reason. It's just made to make scratch web-apps, to try out some idea very quick, or something like that.

Licensing & Conclusion

Do whatever you want with this code. I really don't care. Modify it and sell it, whatever.

Or, improve it, and send your contributions back to me!

The more people that take an interest in this, the more excited I will become about it. So, let me know if you like it. And, further, imitation is the most sincere form of flattery. So, please, imitate. Just mention me when you do; I'll get a kick out of it.

Here are some ideas for improvements:

Any rate, here's hoping you like it!