37
Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014 BCHB524 - 2014 - Edwards

Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

Embed Size (px)

Citation preview

Page 1: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

Web-Applications:TurboGears II

BCHB5242014

Lecture 26

12/03/2014 BCHB524 - 2014 - Edwards

Page 2: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 2

Last time…

We made the empty HoyaTaxa website and learned how to make minor changes, and send dynamic content to template.

We added a “taxa” information page with: Clickable Parent link (Variable number of) clickable Children links

Page 3: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 3

Start web-app and check

Check that the web-application is working... In the class Command-Line shell:

[student@localhost ~]$ cd HoyaTaxa [student@localhost HoyaTaxa]$ start-hoyataxa.py

Start a web-browser and access by urls:http://localhost:8080/http://localhost:8080/taxa/9606http://localhost:8080/taxa?taxid=9606

Page 4: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 4

Empty landing page

Page 5: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 5

Taxonomy page

Page 6: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 6

Tour the primary filesThe files we modified in the HoyaTaxa folder: Controller:

hoyataxa/controllers.py Change the index method Add the taxa method to lookup and return Taxonomy given taxid

(Data) Model: hoyataxa/model.py

SQLObject classes devdata.sqlite

download (or populate) the data in the sqlite database View / Template:

hoyataxa/templates/welcome.html Remove all but the dynamic title

hoyataxa/templates/master.html Change header and footer, remove menus

hoyataxa/templates/taxa.html Set-up the taxa page layout

Page 7: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 7

List all names

Page 8: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 8

List all names

Page 9: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 9

Remove the scientific name

Page 10: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 10

Remove scientific name

Page 11: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 11

Let's add the lineage

Page 12: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 12

Let’s add the lineage

Page 13: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 13

Let’s add the lineage

Page 14: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 14

Empty landing page

Page 15: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 15

Set up search form

In controllers.py, define a class for the formfrom turbogears import validate, validatorsfrom turbogears import widgets, error_handler

class SearchFields(widgets.WidgetsList):    query = widgets.TextField(label="Search Term")    mode = widgets.SingleSelectField(label="Search Mode",                                     options=["Starts with",                                              "Ends with",                                              "Contains"],                                     default="Contains")

search_form = widgets.TableForm(    fields = SearchFields(),    action = "search",    submit_text = "Search"    )

class Root(controllers.RootController):    @expose(template="hoyataxa.templates.welcome")    def index(self):        return dict(form=search_form,                    title="All your taxa are belong to us")

Page 16: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 16

Set up search form

Place the form in welcome.html

Page 17: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 17

Set up search form

Page 18: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 18

Handle the search request

In controllers.py, we add the search method

Page 19: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 19

Handle the search request

Save taxa.html as search.html and modify

Page 20: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 20

Handle the search request

Save taxa.html as search.html and modify

Page 21: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

Search for name: gorilla

12/03/2014 BCHB524 - 2014 - Edwards 21

Page 22: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 22

But…

There is a problem. What to do about bad input?

Too short, spaces at beginning or end…

TurboGears provides validators to check values in the fields to make sure they are OK

Nice integration with form widgets Users get error messages so they can fix the

error

Page 23: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 23

Validation “Schema”

Page 24: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 24

Handle errors in search parameters

Page 25: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 25

Problem is communicated to user

Page 26: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 26

Problem is communicated to user

Page 27: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 27

Validators can be quite complicated

Page 28: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 28

Problem is communicated to user

Page 29: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 29

Setup for use by web-services

Our web-site can now be accessed programatically… …as we did with urllib.urlopen in python

Access:http://localhost:8080/taxa?taxid=9606

http://localhost:8080/search?query=hum&mode=Contains

However, we usually don’t want to parse HTML. Programs want to parse “easy” no-frills formats.

Page 30: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 30

Let’s provide XML output format

We need a new output template for search: searchxml.html

Page 31: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 31

XML output format

Next we need to tell the search method when to use it…

Page 32: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 32

XML Output Format

To get XML format output, add “&tg_format=xml” to end of URL.

Try it:

http://localhost:8080/search?query=hum&mode=Contains

http://localhost:8080/search?query=hum&mode=Contains&tg_format=xml

Page 33: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 33

Similarly for the taxa page

Page 34: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 34

Similarly for the taxa page

Page 35: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 35

XML Output Format

To get XML format output, add “tg_format=xml” to end of URL.

Try it:

http://localhost:8080/taxa?taxid=9606http://localhost:8080/taxa?taxid=9606&tg_format=xml

http://localhost:8080/taxa/9606http://localhost:8080/taxa/9606?tg_format=xml

Page 36: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 36

All done…

We can now display a taxonomy record nicely if the user types a URL ...and then navigate about its heirachy.

Can search the names based on a user query. Search form, list of matching results, etc...

XML output for web-services.

Page 37: Web-Applications: TurboGears II BCHB524 2014 Lecture 26 12/03/2014BCHB524 - 2014 - Edwards

12/03/2014 BCHB524 - 2014 - Edwards 37

TODO…

If only one matching search result – jump straight to taxa page… Even if only one taxa matches?

Search/lookup by taxid too? Make pages and tables prettier

Center and position tables on page Alternate row colors