Compare commits

...

4 Commits

@ -0,0 +1,27 @@
from query_materials import get_info, query
from time import sleep
q1 = """
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
SELECT DISTINCT ?item ?itemLabel WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
{
SELECT DISTINCT ?item WHERE {
?item p:P1343 ?statement0.
?statement0 (ps:P1343/(wdt:P279*)) wd:Q19219752.
?item p:P31 ?statement1.
?statement1 (ps:P31/(wdt:P279*)) wd:Q16521.
?item p:P4000 ?statement2.
?statement2 (ps:P4000/(wdt:P279*)) wd:Q5118786.
}
LIMIT 100
}
}
"""
for result in query(q1):
info = get_info(result['item']['value'])
print('A ' + info['labels']['en']['value'] + ' IS KNOWN IN DUTCH AS ' + info['labels']['nl']['value'])
print('A ' + info['labels']['en']['value'] + ' TREE')
sleep(1)

@ -30,7 +30,9 @@ It is most likely Python isn't installed. You can [download it, and find install
### Jinja
```python
pip3 install jinja2
```
## Running the script
@ -38,7 +40,7 @@ Running the script should result in the folder of the script: `index.html` this
*When using the terminal*: The script can be ran by first using `cd` to move into the folder of the script. The easiest way is typing `cd` and a space and then drag and drop the folder onto the terminal. Then press enter. Once you are in the folder, type:
```
```bash
python3 render-template.py
```
@ -49,7 +51,7 @@ Pages using paged.js *and* an external styles file need to be opened through a l
It is quite possible your editor has this functionality built in and it is perfectly fine to use that.
Python also has a simple server built in. To start it, first `cd` into the folder of your project. Then run:
```
```bash
python3 -m http.server
```
@ -68,13 +70,13 @@ Open a webbrowser and open the address <http://0.0.0.0:8000/> to see the generat
It is possible to use a different template by adjusting the `env.get_template` replace for example the line:
```
```python
template = env.get_template("template.html")
```
with:
```
```python
template = env.get_template("template-pagedjs.html")
```
@ -89,13 +91,13 @@ Extensive documentation on the jinja templating engine can be found here: <https
The first way to extend the template is by providing extra variables, this can be done by providing extra keyword arguments in the template render call. With the python line below a placeholder `extra_placeholder` is added and becomes available in the template:
```
```python
rendered_template = template.render(template_placeholder="world", extra_placeholder="More information in the template")
```
In the template it is possible to refer to this placeholder with the `{{ }}` syntax, so:
```
```jinja
{{ extra_placeholder }}
```
@ -105,19 +107,19 @@ Just like python jinja supports [loops](https://jinja.palletsprojects.com/en/3.1
It is important to understand that jinja places data in a template. It is not intended to generate data. To work with loops jinja needs a list (or actually an iterable), data it can loop over. In jinja the start of a loop is indicated with `{% for %}` the end of the loop with `{% endfor %}`
Imagine this render call:
Imagine this render call in `render-template.py`:
```
```python
rendered_template = template.render(template_placeholder="world", list_of_technologies=["python", "jinja", "html", "css", "javascript", "paged.js"])
```
Now, add the following lines to the template to make jinja loop through the list and show the indvidual items in an HTML-list:
```
```jinja
<h2>Used technologies</h2>
<ul>
{% for technology in list_of_technologies %}
<li>{{ technology }}</li>
{% endfor %}
</ul>
```
```

@ -38,22 +38,22 @@ SELECT DISTINCT ?item ?statement0 ?material WHERE
LIMIT 10
"""
print (q1)
print ()
from time import sleep
for result in query(q1):
statement = result['statement0']['value']
value, item = result['material']['value'], result['item']['value']
value = get_info(value)
item = get_info(item)
# print ("value", value)
# print ("item", item)
try:
material_label, item_label = value['labels']['en']['value'], item['labels']['en']['value']
print (f"A {item_label} made of {material_label}. See {statement}")
print ()
sleep(3)
except KeyError:
print ("no english label?")
if __name__ == '__main__':
print (q1)
print ()
from time import sleep
for result in query(q1):
statement = result['statement0']['value']
value, item = result['material']['value'], result['item']['value']
value = get_info(value)
item = get_info(item)
# print ("value", value)
# print ("item", item)
try:
material_label, item_label = value['labels']['en']['value'], item['labels']['en']['value']
print (f"A {item_label} made of {material_label}. See {statement}")
print ()
sleep(3)
except KeyError:
print ("no english label?")
Loading…
Cancel
Save