How to get only value AAA in sparql result (rdflib.term.Literal('AAA'),) - rdflib

Here is my problem:
I'm trying using sparql to get the value and show only result for web user
the problem is i only know how to get result include rdflib.term.Literal('AAA')
Is there any way to get only the value AAA without rdflib.term.Literal ?
The code i use in python:
import rdflib
graph = rdflib.Graph()
graph.parse ("static/owl/rdfxmlontologyhotrobenhtieuduong.owl")
query = """PREFIX : <http://www.semanticweb.org/ngocv/ontologies/2020/5/hotrobenhtieuduong#>
SELECT ?a
WHERE { <http://www.semanticweb.org/ngocv/ontologies/2020/5/hotrobenhtieuduong#Benhnhan001>:CoHoVaTen ?b
BIND(STR(?b) AS ?a)
}"""
#app.route('/testsql')
def testsql():
return render_template("testsql.html", values1=users.query.all(), values2=admins.query.all())
#app.route('/testontology')
def testontology():
r = graph.query(query)
return render_template("testontology.html", values=r)
Code in testontology.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test ontology</title>
</head>
<body>
<h1>Test Ontology</h1>
{{values}}
{% for item in values %}
<p>Class ontology: {{ item }}</p>
{% endfor %}
</body>
</html>
Result I get:
Test Ontology
<rdflib.plugins.sparql.processor.SPARQLResult object at 0x0000027DD9F39400>
Class ontology: (rdflib.term.Literal('Nguyen Minh Anh'),)
For the ontology I used to query
Ontology
I found a post on stackoverflow (How can I use the RDFLIB module in Python to retrieve a value from an OWL file using SparQL?)
the solution is print row['value'] but i cannot apply that solution to my code

str(r[‘a’])
You can get the literal valubof any RDFliteral by just casti it to a string with Python’s str() method so just pass the value as above, not just r, to the template.

Related

Write all files in a directory to .gsp page

Lets say I have a gsp page that I want to load all the scripts in a particular folder:
<html>
<head>
{each file in $path}
<asset:javascript src="file" />
</head>
</html>
assuming $path is a directory path. Most templating languages have a way to do this so I'm sure Grails can accomplish it. But I'm not sure how to make it happen. My end goal is this:
ndex
<html>
<head>
<asset:javascript src="js/util.js" />
<asset:javascript src="js/util2.js" />
<asset:javascript src="js/index.js" />
</head>
</html>
Please help.
You can do something like this:
<html>
<head>
<g:each var="file" in="${(new File(path)).listFiles()*.path}">
<asset:javascript src="${file}" />
</g:each>
</head>
</html>
The GSP g:each tag is how iteration is performed in Grails when using GSP. The in attribute is used to specify the Iterable to iterate through. In this case it's the expression:
(new File(path)).listFiles()*.path
The expression means:
new File(path) - Create a Java File object to represent the directory path.
.listFiles() - Returns a list of all files and directories (excluding sub-directories) each represented by File objects.
*.path - A spread operator expression which returns a list of file path Strings, effectively converting the File objects into Strings. It's the equivalent of .collect { it.path }.

How can I dynamically add an attribute to a body element in a GSP page

In Grails 2.3.7, is there a way to use an expression in a GSP page to add an attribute to a body element? In the code below, the expression in the p element works, but the same expression in the body element causes a error: Expecting '=' after attribute name (${raw('this="that"')}).
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body ${raw('this="that"')}>
<p ${raw('this="that"')}>Hello!</p>
</body>
</html>
I'm trying to do this in a layout and pick up the corresponding attribute from the original page with pageProperty, but the same error occurs on the body element in the page layout as well.
Replacing an attribute value does work in a body element like this:
<body this="${that}">
but this won't work because I do not want the attribute to appear at all if it has no value.
the problem is, that the body tag is replaced by the <g:layoutBody /> tag and therefore could not be set like this.
One solution is to use to set different stuff in the sitemesh layout.
An example of this is shown here:
<html>
<head>
<g:layoutHead/>
</head>
<body class="${pageProperty( name:'body.class' )}">
<g:layoutBody/>
</body>
</html>

Repeating over a list Polymer broken

Update: problem with bleeding edge editor not being compatible with the version of serialization/polymer on pub. Need to install versions from SVN.
Repeating over a list appears to have been broken to me.
https://github.com/sethladd/dart-polymer-dart-examples/blob/master/web/bind_and_repeat_over_list_of_primitives/
This is the simplest example I could find and I get the same error:
Internal error: 'package:serialization/src/serialization_helpers.dart': Error: line 212 pos 7: unresolved implicit call to super constructor 'LinkedHashMap()'
class IdentityMap extends LinkedHashMap {
^
Probably this update from #dartlang 10 Sep 2013 :-
'New Dart release with search improvements in Editor, and more.
SDK changes include:
HashMap and LinkedHashmap cannot be extended anymore......'
Looks as though Polymer needs updating for this.
You can modify the code slightly and make it work.
Change the index.html file to look like this:
<!DOCTYPE html>
<html>
<head>
<title>index</title>
<script src="packages/polymer/boot.js"></script>
</head>
<body>
<ul>
<template id="tmpl" bind>
<template repeat="{{}}">
<li>{{}}</li>
</template>
</template>
</ul>
<script type="application/dart" src="index.dart"></script>
</body>
</html>
And your index.dart should look like this (it is unchanged):
import 'dart:html';
main() {
List fruits = ['apples', 'oranges', 'pears'];
query('#tmpl').model = fruits;
}
You bind to a template that contains the template where the repeat is registered. the {{}} in repeat="{{}}"refers to the bound value. The {{}} in the <li>{{}}</li> refers to each item in the list.
Hope this helps.

Multi-lines string in Dart

I'm still working on Dart. I just want to output my string defined in .dart to my html. I look into the documentation on https://sites.google.com/site/dartlangexamples/learn/variables/strings
I understand that I need to use the """.
When I print my query, I got the good result, but when I output it in html, They are side to side.
There it is:
.dart :
var symbole = """
*
***
*****
*******
*********""";
var element03 = query('#exercice03');
element03.innerHTML = symbole;
print(symbole);
}
The Print give me exactly what I set into my var symbole BUT, in my HTML i got this:
My html is :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HelloWorld</title>
<link rel="stylesheet" href="HelloWorld.css">
</head>
<body>
<div id="container">
<p class="question">Exercice : Pritn the * : <span id="exercice03"></span></p>
</div>
<script type="application/dart" src="web/HelloWorld.dart"></script>
<script src="https://dart.googlecode.com/svn/branches/bleeding_edge/dart/client/dart.js"></script>
</body>
</html>
I don't understand why in my output html I dont get the same result as my Print in my dart editor, can some one help me to figure this out?
I'm not sure if I follow you here -- do you want this?
var symbole = """
*<br />
***<br />
*****<br />
*******<br />
*********<br />""";
var element03 = query('#exercice03');
element03.innerHTML = symbole;
I just added the break lines
So as Matt B said, the output of print() is differently formatted than HTML on the browser.
This is because HTML is not whitespace sensitive. That is, a new line in html does not display when parsed. You need to use a line break <br> or you need to wrap it in a preformated <pre> tag

jQuery UI Autocomplete can't figure it out

I decided to use jQuery UI for my autocomplete opposed to a plugin because I read that the plugins are deprecated. My overall goal is to have an autocomplete search bar that hits my database and returns users suggestions of city/state or zipcodes in a fashion similar to google. As of now I am not even sure that the .autocomplete function is being called. I scratched everything I had and decided to start with the basics. I downloaded the most recent version of jQuery UI from http://jqueryui.com/download and am trying to get the example that they use here http://jqueryui.com/demos/autocomplete/ to work. All the scripts that I have included seem to be connected at least linked through Dreamworks so I am fairly certain that the paths I have included are correct. The CSS and Javascripts that I have included are unaltered straight from the download. Below is my HTML code and my backend PHP code that is returning JSon formated data. Please help me. Maybe I need to include a function that deals with the JSon returned data but I am trying to follow the example although I see that they used a local array.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQueryUI Demo</title>
<link rel="stylesheet" href="css/ui-lightness/jquery-ui-1.8.17.custom.css" type="text/css" />
<script type="text/javascript" src ="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src ="js/jquery-ui-1.8.17.custom.min.js"></script>
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#tags").autocomplete({
source: "search_me.php"
});
});
</script>
</head>
<body>
<div class="demo">
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags" />
</div>
</div><!-- End demo -->
<div class="demo-description">
<p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are tags for programming languages, give "ja" (for Java or JavaScript) a try.</p>
<p>The datasource is a simple JavaScript array, provided to the widget using the source-option.</p>
</div><!-- End demo-description -->
</body>
</html>
Below the PHP part.
<?php
include 'fh.inc.db.php';
$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or
die ('Unable to connect. Check your connection parameters.');
mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db));
$location = htmlspecialchars(trim($_GET['term'])); //gets the location of the search
$return_arr = array();
if(is_numeric($location)) {
$query = "SELECT
zipcode_id
FROM
user_zipcode
WHERE
zipcode_id REGEXP '^$location'
ORDER BY zipcode_id DESC LIMIT 10";
$result = mysql_query($query, $db) or die(mysql_error($db));
while($row = mysql_fetch_assoc($result)) {
extract($row);
$row_array['zipcode_id'] = $zipcode_id;
array_push($return_arr, $row_array);
}
}
mysql_close($db);
echo json_encode($return_arr);
?>
Thanks for the ideas. Here is an update.
I checked the xhr using firebug and made sure that it is responding thanks for that tip. also the above php code I hadn't initialized $return_arr so i took care of that. Also thanks for the clarification of the js required or rather not required. Now when I type in a zipcode a little box about a centimeter shows up underneath it but I can't see if anything is in there, I would guess not. I went to my php page and set it up to manually set the variable to "9408" and loaded the php page directly through my browser to see what it returned. This is what it returned.
[{"zipcode_id":"94089"},{"zipcode_id":"94088"},{"zipcode_id":"94087"},{"zipcode_id":"94086"},{"zipcode_id":"94085"},{"zipcode_id":"94083"},{"zipcode_id":"94080"}]
I then went to a JSON code validator at this url http://jsonformatter.curiousconcept.com/ at it informed me that my code is in fact returning JSON formatted data. Anymore suggestions to help me troubleshoot the problem would be terrific.
Wow after more research I stumbled across the answer on someone another post.
jquery autocomplete not working with JSON data
Pretty much the JSON returned data must contain Label or Value or both. Switched the zipcode_id to value in my $row_array and... boom goes the dynamite!
Your scripts (js files) references are not correct, should only be:
<!-- the jquery library -->
<script type="text/javascript" src ="js/jquery-1.7.1.min.js"></script>
<!-- the full compressed and minified jquery UI library -->
<script type="text/javascript" src ="js/jquery-ui-1.8.17.custom.min.js"></script>
The files "jquery.ui.core.js", "jquery.ui.widget.js" and "jquery.ui.position.js" are the separated development files, the jquery ui library is splitted into modules.
The file "jquery-ui-1.8.17.custom.min.js" contains them all, compressed and minified !
Concerning the data source, as stated in the "Overview" section of the Autocomplete documentation: when using a an URL, it must return json data, either of the form of:
an simple array of strings: ['string1', 'string2', ...]
or an array of objects with label (and a value - optionnal) property [{ label: "My Value 1", Value: "AA" }, ...]
I'm really not familiar with PHP so just make sure your php script returns one of those :-)

Resources