![]() Priority queue and heap queue data structure Python Object Serialization - yaml and json Python Object Serialization - pickle and json Sets (union/intersection) and itertools - Jaccard coefficient and shingling to check plagiarismĬlasses and Instances (_init_, _call_, etc.)īits, bytes, bitstring, and constBitStream Strings - Escape Sequence, Raw String, and Slicingįormatting Strings - expressions and method calls Object Types - Numbers, Strings, and None Running Python Programs (os, sys, import) This tells the template to insert the actual value of the variable right in place. If we need to access a variable within a non-Python code line inside the template, we need to put it into double curly braces. The first for statement accesses the tuples within the list, the second one the items within the tuple, which are put each into a cell of the table. The rows is a variable which holds the result of the database query, so it is a list of tuples. The other lines are plain HTML.Īs we can see, we use Python's for statement two times, in order to iterate over rows. Please note that, of course, only valid Python statements are allowed, otherwise the template will raise an exception. The line starting with % is interpreted as Python code. So, we need to write the wish_table.tpl template, which should like this: If we run the server at this stage and calls we get the following error: Note that templates always return a list of strings, thus there is no need to convert anything. In addition to calling the template, we assign result, which we received from the database query, to the variable rows, which is later on used within the template. We returned the output of the template wish_table.Return template('wish_table', rows=result) To incorporate the template in our example, we just add the following lines: The result set is, as we've seen in the previous picture, a list of tuples, each tuple contains one set of results. In this section, we are going to put the result of our query showing the open bucket list items into a simple table with two columns: the first column will contain the id of the item, the second column the list. the result set of a database query, which will be then formatted within the template. Furthermore, templates can take arguments, e.g. Template can contain any type of text (which will be most likely HTML mixed with Python statements). The template can be called from within a function. However, it won't work if we bind one route to more than one function.Ī template in bottle is stored as separate file with a. So the following code will bucket_list(): What we see is a list of tuples with each tuple contains one set of results returned from the SQL query.Īctually, we can bind more than one route to a function. Let's just run python bucket.py and call the page in our browser: By doing this, we bind this function to the route /bucket, so every time the browsers calls Bottle returns the result of the function bucket_list(). The important point is the decorator statement right before the def bucket_list() statement. Note that we defined one function, bucket_list(), with a few lines of code reading from the database. The imported route is responsible for Bottle's routing. By default, the web server serves the pages on 'localhost' and 'port 8080'. The run() starts the web server included in Bottle. If so, Bottle will execute the corresponding Python code and return its result.įrom bottle import route, bucket_list():Ĭ.execute("SELECT id, wish FROM bucket WHERE status LIKE '1'") When the page is used as the url for the browser, Bottle takes the call and checks if there is any (Python) function defined for the route 'bucket'. Finally, the column status is used to mark a task as open (value 1) or closed (value 0). The column wish holds the text which describes the wish, it can be max 100 characters long. ![]() id is a unique id for each row, which is used later on to reference the rows. ![]() This will generates a database-file bucket.db with tables called bucket and three columns id, wish, and status. ![]() First, we need to make a db using SQLite:Ĭon.execute("CREATE TABLE bucket (id INTEGER PRIMARY KEY, wish char(100) NOT NULL, status bool NOT NULL)")Ĭon.execute("INSERT INTO bucket (wish,status) VALUES ('Flying over Golden Gate Bridge',0)")Ĭon.execute("INSERT INTO bucket (wish,status) VALUES ('Wind surfing under Golden Gate Bridge',0)")Ĭon.execute("INSERT INTO bucket (wish,status) VALUES ('Bungee jumping from Golden Gate Bridge',0)")Ĭon.execute("INSERT INTO bucket (wish,status) VALUES ('Walking across Golden Gate Bridge',1)")īottle v0.12.7 server starting up (using WSGIRefServer()).ġ27.0.0.1 - "GET /api/status HTTP/1.1" 200 53 ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |