Skip to content

Commit

Permalink
moving wdunits into place
Browse files Browse the repository at this point in the history
  • Loading branch information
stuchalk committed Jan 1, 2025
1 parent 1a11413 commit 847c244
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 65 deletions.
7 changes: 3 additions & 4 deletions dashboard/example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# import json
""" code snippets to migrate/ingest data """
import os
import django

Expand All @@ -9,7 +9,7 @@
from units.functions import *
from wdfunctions import *

choice = 'wdu'
choice = 'wduqks'

local = timezone("America/New_York")

Expand Down Expand Up @@ -836,7 +836,6 @@
for sicls in siclss:
print(sicls)


# check wdquants data against the quantities data
if choice == 'wdqchk':
qs = Wdquants.objects.all().values('quant', 'sect')
Expand Down Expand Up @@ -872,7 +871,7 @@
unts.update({u['unit']: u['id']})

units = None
file = f'umis_units_query_121724.json'
file = f'umis_units_query.json'
if os.path.exists(os.path.join(BASE_DIR, STATIC_URL, file)):
# read in the file (open function has read as default so not added)
with open(os.path.join(BASE_DIR, STATIC_URL, file)) as f:
Expand Down
3 changes: 3 additions & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
</li>
</ul>
</div>
<form id="uform" action="/search" class="d-flex" role="search">
<input name="q" class="form-control form-control-sm me-2" type="search" placeholder="Search Units" aria-label="Search Units">
</form>
</div>
</nav>
</header>
Expand Down
7 changes: 7 additions & 0 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ <h4 dir="rtl">Common Unit Representations</h4>
</ul>
</div>
</div>
<div class="card bg-light p-3 mb-2">
<p class="mb-0 justify-content">This service uses the following open source Python packages:</p>
<ul>
<li>qwikidata: https://github.com/kensho-technologies/qwikidata</li>
<li>ucumvert: https://github.com/dalito/ucumvert</li>
</ul>
</div>
<div class="well-sm">
<p style="font-style: italic;font-weight: bold;">This <a href="/static/files/Semantic-Units-Project.pdf">project</a> was funded by NIST through award #70NANB17H209 (2017-2021)</p>
</div>
Expand Down
76 changes: 55 additions & 21 deletions templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,59 @@
{% block title %} UMIS Search {% endblock %}
{% block header %} UMIS Search {% endblock %}
{% block content %}
<h2>Search Results for '{{ term }}'</h2>
<div class="row mt-2">
<div class="col-12">
{% if hits.units %}
<h3>Units</h3>
<ol>
{% for k, v in hits.units.items %}
<li><a href="/units/view/{{ k }}">{{ v }}</a></li>
{% endfor %}
</ol>
{% endif %}
{% if hits.quants %}
<h3>Quantities</h3>
<ol>
{% for k, v in hits.quants.items %}
<li><a href="/quantities/view/{{ k }}">{{ v }}</a></li>
{% endfor %}
</ol>
{% endif %}
</div>
</div>
<h2>Search Results for '{{ term }}'</h2>
{% if hits.units %}
<div class="col-12 mb-3">
<div class="card card-success">
<div class="card-header">
<h4 class="card-title mb-0">Units of Measure ({{ hits.units|length }})</h4>
</div>
<div class="card-body" style="max-height: 300px; overflow-y: scroll;">
<div class="row">
<div class="col-12">
{% for k, v in hits.units.items %}
<a class="btn btn-sm btn-outline-primary my-1" href="/units/view/{{ k }}">{{ v }}</a>
{% endfor %}
</div>
</div>
</div>
</div>
</div>
{% endif %}
{% if hits.quants %}
<div class="col-12 mb-3">
<div class="card card-success">
<div class="card-header">
<h4 class="card-title mb-0">Quantities ({{ hits.quants|length }})</h4>
</div>
<div class="card-body" style="max-height: 300px; overflow-y: scroll;">
<div class="row">
<div class="col-12">
{% for k, v in hits.quants.items %}
<a class="btn btn-sm btn-outline-success my-1" href="/quantities/view/{{ k }}">{{ v }}</a>
{% endfor %}
</div>
</div>
</div>
</div>
</div>
{% endif %}
{% if hits.reps %}
<div class="col-12">
<div class="card card-success">
<div class="card-header">
<h4 class="card-title mb-0">Unit Representations ({{ hits.reps|length }})</h4>
</div>
<div class="card-body" style="max-height: 300px; overflow-y: scroll;">
<div class="row">
<div class="col-12">
{% for k, v in hits.reps.items %}
<a class="btn btn-sm btn-outline-danger my-1" href="/units/view/{{ k }}">{{ v }}</a>
{% endfor %}
</div>
</div>
</div>
</div>
</div>
{% endif %}
{% endblock %}
3 changes: 2 additions & 1 deletion templates/units/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ <h2 class="left">Units of Measurement <small>(by Quantity Kind)</small></h2>
<td><b>{{ key }}</b>:
<span class="units">
{% for unit in units %}
<a class="unit item" data-content="{{ unit.name|lower }}" href="/units/view/{{ unit.id }}"> {{ unit.name }} </a>
<a class="unit item" data-content="{{ unit.name|lower }}" href="/units/newview/{{ unit.id }}">
{% if forloop.counter > 1 %}•{% endif %} {{ unit.name }}</a>
{% endfor %}
</span>
</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ <h2 class="left">Units of Measurement <small>(by Quantity Kind)</small></h2>
<td><b>{{ key }}</b>:
<span class="units">
{% for unit in units %}
<a class="unit item" data-content="{{ unit.name|lower }}" href="/units/newview/{{ unit.id }}">
{% if forloop.counter > 1 %}•{% endif %} {{ unit.name }}</a>
<a class="unit item" data-content="{{ unit.name|lower }}" href="/units/view/{{ unit.id }}"> {{ unit.name }} </a>
{% endfor %}
</span>
</td>
Expand Down
12 changes: 8 additions & 4 deletions templates/units/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ <h4 class="card-title mb-0">Unit: {{ unit.name }} {{ unit.unit }}</h4>
<div class="card-body">
<div class="row">
<div class="col-5">
<b>Unit System</b>:
{% if usyss %}
<b>Unit System</b>:
{% for usys in usyss %}
{% if forloop.counter > 1 %}, {% endif %} <a href="/unitsystems/view/{{ usys.unitsystem.id }}">{{ usys.unitsystem.name }}</a>
{% endfor %}
Expand All @@ -22,13 +22,17 @@ <h4 class="card-title mb-0">Unit: {{ unit.name }} {{ unit.unit }}</h4>
{% endif %}
<br/>
<b>Unit Type</b>: {{ unit.type }}{{ unit.sitype.silabel }}<br/>
{% if qsys %}<b>Quantity System</b>: <a href="/quantitysystems/view/{{ qsys.id }}">{{ qsys.name }}</a><br/>{% endif %}
<b>Quantity Kind(s)</b>:
{% if qsys %}
<b>Quantity System</b>: <a href="/quantitysystems/view/{{ qsys.id }}">{{ qsys.name }}</a><br/>
{% endif %}
{% if qkinds %}
<b>Quantity Kind(s)</b>:
{% for qkind in qkinds %}
<a href="/quantitykinds/view/{{ qkind.quantitykind.id }}">{{ qkind.quantitykind.name }}</a>{% if not forloop.last %}, {% endif %}
{% endfor %}
<br/>
<b>Dimension</b>: {{ dv.symbol|safe }}<br/>
{% endif %}
<b>Dimension</b>: {{ dv|safe }}<br/>
</div>
<div class="col-7 text-end">
{% if quants|length > 0 %}
Expand Down
7 changes: 4 additions & 3 deletions units/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
path("", views.home, name='homepage'),
path("search", views.search, name='search'),
path("units", views.index, name='index'),
path("units/index", views.index, name='index'),
path("units/newindex", views.newindex, name='new index'),
path("units/newview/<uid>", views.newview, name='new view'),
path("units/", views.index, name='index'),
path("units/oldindex", views.oldindex, name='index'),
path("units/index", views.index, name='wd index'),
path("units/view/<uid>", views.view, name='view'),
path("units/unitimport/", views.unitimport, name='import'),
path("units/crosswalk/", views.crosswalk, name='crosswalk'),
path("units/crosswalk/<sys1id>/<sys2id>", views.crosswalk, name='crosswalk'),
Expand Down
65 changes: 35 additions & 30 deletions units/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
from django.http import JsonResponse
from units.models import *


def home(request):
""" return the homepage """
return render(request, "../templates/home.html")


def index(request):
def oldindex(request):
""" present an overview page about the system in the sds """
data = Units.objects.all().order_by('quantitykindsunits__quantitykind__name', 'name')
byq = {}
Expand All @@ -28,10 +27,11 @@ def index(request):
tmp2 = list(byq.keys())
tmp2.sort()
data = {i: byq[i] for i in tmp2}
return render(request, "../templates/units/index.html", {'data': data})
return render(request, "../templates/units/oldindex.html", {'data': data})


def newindex(request):
def index(request):
""" new index function getting data from wikidata tables"""
raw = Wdunits.objects.all().order_by('wdclass__quant', 'unit')
data = {}
for u in raw:
Expand All @@ -43,10 +43,10 @@ def newindex(request):
data.update({quant: []})
unit = {'id': u.id, 'name': u.unit}
data[quant].append(unit)
return render(request, "../templates/units/newindex.html", {'data': data})
return render(request, "../templates/units/index.html", {'data': data})


def view(request, uid):
def oldview(request, uid):
""" view the different representations of a unit"""
if uid.isnumeric():
try:
Expand All @@ -65,7 +65,7 @@ def view(request, uid):
quants = []
types = []
for qkind in qkinds:
dvs.append(qkind.quantitykind.dimensionvector)
dvs.append(qkind.quantitykind.dimensionvector.symbol)
types.append(qkind.quantitykind.type)
for quant in qkind.quantitykind.quantities_set.all():
quants.append(quant)
Expand Down Expand Up @@ -105,7 +105,7 @@ def view(request, uid):
'dv': dv, 'corsf': corsf, 'corst': corst, 'qsys': qsys, 'quants': quants, 'type': typ})


def newview(request, uid):
def view(request, uid):
""" view the different representations of a unit"""
if uid.isnumeric():
try:
Expand All @@ -118,13 +118,17 @@ def newview(request, uid):
except Wdunits.DoesNotExist:
return redirect('/')
wdunit = Wdunits.objects.get(id=uid)
quants = wdunit.wdquantswdunits_set.all()
qids = wdunit.wdquantswdunits_set.all().values_list('wdquant__quant__id', flat=True)
dvs = wdunit.wdquantswdunits_set.all().values_list('wdquant__isq', flat=True)
dvs = list(set(dvs))
dv = dvs[0]
dv = dv.replace('^', '').replace('{', '<sup>').replace('}', '</sup>').replace('\Theta', 'Θ')
quants = Quantities.objects.filter(id__in=qids)
data = wdunit.representations_set.all()
usyss = wdunit.unitsystemswdunits_set.all()
qkinds = None
equsf = None
equst = None
dv = None
corsf = None
corst = None
qsys = None
Expand Down Expand Up @@ -154,35 +158,36 @@ def search(request):
""" search of the unit strings """
term = request.GET.get("q")
if term:
hits = {}

# find units with term in string representation
strngs = Strngs.objects.filter(string__icontains=term).values_list('id', flat=True)
if strngs:
units = Units.objects.filter(representations__strng_id__in=strngs).distinct()
for unit in units:
if 'units' not in hits.keys():
hits.update({'units': {}})
hits['units'].update({unit.id: unit.name + " (in representation)"})
hits = {'units': {}, 'quants': {}, 'reps': {}}

# find units with term in unit name
units = Units.objects.filter(name__icontains=term)
for unit in units:
if 'units' not in hits.keys():
hits.update({'units': {}})
hits['units'].update({unit.id: unit.name})
units = Wdunits.objects.filter(unit__icontains=term)
if units:
for unit in units:
hits['units'].update({unit.id: unit.unit})

# find quantities with the term in the name
quants = Quantities.objects.filter(name__icontains=term)
if quants:
for quant in quants:
if 'quants' not in hits.keys():
hits.update({'quants': {}})
hits['quants'].update({quant.id: quant.name})

# find representations with the term
if len(term) > 2: # if long string then search as substring
reps = Representations.objects.filter(strng__string__icontains=term)
else: ## ...otherwise search as exact
reps = Representations.objects.filter(strng__string__exact=term)
if reps:
for rep in reps:
hits['reps'].update({rep.wdunit_id: rep.strng.string})

if hits:
hits['units'] = dict(sorted(hits['units'].items(), key=lambda item: item[1]))
hits['quants'] = dict(sorted(hits['quants'].items(), key=lambda item: item[1]))
# if hits['units']:
# hits['units'] = dict(sorted(hits['units'].items(), key=lambda item: item[1]))
# if hits['quants']:
# hits['quants'] = dict(sorted(hits['quants'].items(), key=lambda item: item[1]))
# if hits['reps']:
# hits['reps'] = dict(sorted(hits['reps'].items(), key=lambda item: item[1]))
return render(request, "../templates/search.html", {'hits': hits, 'term': term})
else:
return redirect('/')
Expand Down Expand Up @@ -254,8 +259,8 @@ def crosswalk(request, sys1id=None, sys2id=None):
data = Repsystems.objects.all().values_list('id', 'name').order_by('name')
return render(request, "../templates/units/crosswalk.html", {'data': data})


def unitimport(request):
""" what does this do? """
units = Units.objects.filter(representations__repsystem__id=10)
output = {}
output.update({'system': 'qudt'})
Expand Down

0 comments on commit 847c244

Please sign in to comment.