[Edu-sig] Simplest webapps

kirby urner kirby.urner at gmail.com
Fri Mar 30 14:05:38 EDT 2018


Hi Aivar --

I think it's a fine idea to write simple Python scripts that write HTML
files, which you may then pull up in the browser.

There's no need to put a server behind static web pages.  So, for example,
I'll have my students write a page of bookmarks:

# -*- coding: utf-8 -*-
"""
Created on Wed Nov  4 18:02:30 2015

@author: Kirby Urner
"""

# tuple of tuples
bookmarks = (
    ("Anaconda.org", "http://anaconda.org"),
    ("Python.org", "http://python.org"),
    ("Python Docs", "https://docs.python.org/3/"),
    ("Spaghetti Code", "http://c2.com/cgi/wiki?SpaghettiCode"),
    ("Structured Programming", "http://c2.com/cgi/wiki?StructuredProgramming
"),
    ("Map of Languages", "
http://archive.oreilly.com/pub/a/oreilly//news/languageposter_0504.html"),
    ("XKCD", "http://xkcd.com"),
    )

page = '''\
<!DOCTYPE HTML>
{}
'''

html = """\
<HTML>
<HEAD>
<TITLE>Bookmarks for Python</TITLE>
</HEAD>
<BODY>
<H3>Bookmarks</H3>
<BR />
<UL>
{}
</UL>
</BODY>
</HTML>
""".lower()

the_body = ""
for place, url in bookmarks:
    the_body += "<li><a href='{}'>{}</a></li>\n".format(url, place)

webpage = open("links.html", "w")
print(page.format(html.format(the_body)), file=webpage)
webpage.close()

All you need add to your example is using print() to save to a file, so the
browser has something to open.

I would not call this a "web app" yet it's instructive in showing how
Python can write HTML files.

Kirby



On Wed, Mar 28, 2018 at 12:18 AM, Aivar Annamaa <aivar.annamaa at ut.ee> wrote:

> Hi!
> Let's say my students are able to write programs like this:
>
> name = input("name")
>
> if name == "Pete":
>     greeting = "Hi"
> else:
>     greeting = "Hello!"
>
> print(f"""
> <html>
> <body>
> {greeting} {name}!
> </body>
> </html>
> """)
>
> I'd like to allow them start writing web-apps without introducing
> functions first (most web-frameworks require functions).
>
> It occurred to me that it's not hard to create a wrapper, which presents
> this code as a web-app (input would be patched to look up GET or POST
> parameters with given name).
>
> This approach would allow simple debugging of the code on local machine
> and no extra libraries are required in this phase.
>
> Any opinions on this? Has this been tried before?
>
> best regards,
> Aivar
>
> _______________________________________________
> Edu-sig mailing list
> Edu-sig at python.org
> https://mail.python.org/mailman/listinfo/edu-sig
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20180330/d5fc98a1/attachment.html>


More information about the Edu-sig mailing list