That's my first stackoverflow question.
I started with kivy and bought the book "Crating Apps in Kivy" from Dusty Phillips and all went well till he got to the openweatherapp chapter. (Which after a search other users had problems too...)
There you use the Openweather API to search for a location with a button and it should print the results in a list.
It's not the original code anymore. Others pointed out that the Link to the website changed, you have to use the API key. Of course I linked it correctly, I just typed "myAPIKey" here so it's not accesssable. The JSOn File on the bottom is what the site prints!
Also, the book formated cities in found location like that: d['name], d['sys']['country'] which I guess was also wrong, maybe the site changed here too?
My problem is that the UrlRequest from kivy doesn't seem to load anything. Or the iteration in found location is wrong, I don't know. It always says "KeyError: city" And I guess it's because it doesn't read it correctly at all. I tried a different approach with the python class requests - and it works perfectly fine! But I still want to know why this solution doesn't work.
My Questions: Where is my error with the json iteration? I have really problems with understanding because I'm also new to python.
Also: Why is request in the methode head? Where is it used?
Here the code from the book (I hope everything is typed correctly. If there is still an error with variables please point it out but I typed the code multiple times - my problem will probably be somewhere else...):
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.network.urlrequest import UrlRequest
import json
class AddLocationForm(BoxLayout):
search_input = ObjectProperty()
search_results = ObjectProperty()
def search_location(self):
search_template = "http://api.openweathermap.org/data/2.5/forecast/daily?APPID=myAPIKey=" + "{}"
search_url = search_template.format(self.search_input.text)
request = UrlRequest(search_url, self.found_location)
def found_location(self, request, data):
data = json.loads(data.decode()) if not isinstance(data, dict) else data
cities = ["{} ({})".format(d["city"]["name"], d["city"]["country"])for d in data["list"]]
self.search_results.item_strings = cities
KV File:
AddLocationForm:
<AddLocationForm>:
orientation: "vertical"
search_input: search_box
search_results: search_results_list
BoxLayout:
height: "40dp"
size_hint_y: None
TextInput:
id: search_box
size_hint_x: 50
Button:
text: "Search"
size_hint_x: 25
on_press: root.search_location()
Button:
text: "Current Location"
size_hint_x: 25
ListView:
id: search_results_list
item_strings: []
JSON File:
{
"city": {
"id": 2761369,
"name": "Vienna",
"coord": {
"lon": 16.37208,
"lat": 48.208488
},
"country": "AT",
"population": 0
},
"cod": "200",
"message": 0.0098,
"cnt": 7,
"list": [{
"dt": 1476439200,
"temp": {
"day": 285.58,
"min": 283.71,
"max": 285.58,
"night": 283.71,
"eve": 285.43,
"morn": 285.58
},
"pressure": 985.46,
"humidity": 73,
"weather": [{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}],
"speed": 7.16,
"deg": 154,
"clouds": 0
}, {
"dt": 1476525600,
"temp": {
"day": 287.33,
"min": 282.7,
"max": 291.08,
"night": 285.72,
"eve": 291.01,
"morn": 283.35
},
"pressure": 983.21,
"humidity": 71,
"weather": [{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}],
"speed": 4.51,
"deg": 150,
"clouds": 32,
"rain": 0.23
}, {
"dt": 1476612000,
"temp": {
"day": 286.88,
"min": 283.94,
"max": 287.15,
"night": 283.94,
"eve": 286.44,
"morn": 286.26
},
"pressure": 989.94,
"humidity": 98,
"weather": [{
"id": 501,
"main": "Rain",
"description": "moderate rain",
"icon": "10d"
}],
"speed": 4.36,
"deg": 315,
"clouds": 92,
"rain": 5.51
}, {
"dt": 1476698400,
"temp": {
"day": 287.39,
"min": 283.49,
"max": 287.39,
"night": 284.89,
"eve": 285.05,
"morn": 283.49
},
"pressure": 984.76,
"humidity": 0,
"weather": [{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}],
"speed": 1.64,
"deg": 132,
"clouds": 69,
"rain": 1.55
}, {
"dt": 1476784800,
"temp": {
"day": 285.91,
"min": 283.52,
"max": 285.91,
"night": 283.52,
"eve": 284.45,
"morn": 284.18
},
"pressure": 982.64,
"humidity": 0,
"weather": [{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}],
"speed": 3.23,
"deg": 137,
"clouds": 81,
"rain": 2.4
}, {
"dt": 1476871200,
"temp": {
"day": 283.71,
"min": 282.37,
"max": 283.71,
"night": 282.37,
"eve": 282.52,
"morn": 282.59
},
"pressure": 978.26,
"humidity": 0,
"weather": [{
"id": 502,
"main": "Rain",
"description": "heavy intensity rain",
"icon": "10d"
}],
"speed": 2.68,
"deg": 128,
"clouds": 96,
"rain": 14.37
}, {
"dt": 1476957600,
"temp": {
"day": 286.52,
"min": 282.13,
"max": 286.52,
"night": 282.13,
"eve": 282.72,
"morn": 282.19
},
"pressure": 975.05,
"humidity": 0,
"weather": [{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}],
"speed": 1.47,
"deg": 173,
"clouds": 13,
"rain": 2.83
}]
}
Btw, it's not a duplicate from how do i use json api in my kivy program becaue it didn't really help... The code doesn't work anymore because the API changed.
(If anyone finds this question and is looking for a working URL reader, here is my code. Don't forget to import requests)
def search_location(self):
#Get URL From Weather API + Input
search_template = "http://api.openweathermap.org/data/2.5/forecast/daily?APPID='enteryourweatherAPIKeyhere'&q=" + "{}"
search_url = search_template.format(self.search_input.text)
#Reads the Website and saves it in a string
urlresults = (requests.get(search_url)).text
#Converts Json to Python Dictionary
urlresults = json.loads(urlresults)
cities = ["{} ({})".format(urlresults["city"]["name"], urlresults["city"]["country"])]
Go to this page:
http://openweathermap.org/faq#error401
the API now requires a key. After registering on openweathermap, a key will be mailed to you. Replace the current URL with the one given in the mail.
This worked for me.
Related
Java 11
SonarQube 8.9.2 LTS
For my java project the SonarQube show the next issues info:
Severity
Blocker 1.3k
Minor 1.1k
Critical 5.8k
Info 233
Major 1.3k
So I need to get this information via SonarQube WEB API.
I found only this api method:
GET http://some_url_sonar_qube/api/issues/search
And its return all issues on page = 1
And its return all issues on page = 1 with detail info
{
"total": 10049,
"p": 1,
"ps": 100,
"paging": {
"pageIndex": 1,
"pageSize": 100,
"total": 10049
},
"effortTotal": 50995,
"issues": [
{
"key": "dddd",
"rule": "css:S4670",
"severity": "CRITICAL",
...
This:
GET http://some_url_sonar_qube/api/issues/search?p=2
And its return all issues on page = 2
and so on.
Response example:
As you can see has 10049 issues. It's 100 pages.
But I need summary info. Smt like this in json format:
{
"Severity": {
"Blocker": 1300,
"Minor": 1100,
"Critical": 5800,
"Info": 233,
"Major": 1300
}
}
I'm not found api method for this
I found solution (thanks for #gawkface)
Use this method:
GET http://some_url_sonar_qube/api/issues/search?componentKeys=my_project_key&facets=severities
And here result (on section facets)
{
"total": 10049,
"p": 1,
"ps": 100,
"paging": {
"pageIndex": 1,
"pageSize": 100,
"total": 10049
},
"effortTotal": 50995,
"issues": [...],
"components": [...],
"facets": [
{
"property": "severities",
"values": [
{
"val": "CRITICAL",
"count": 5817
},
{
"val": "MAJOR",
"count": 1454
},
{
"val": "BLOCKER",
"count": 1286
},
{
"val": "MINOR",
"count": 1161
},
{
"val": "INFO",
"count": 331
}
]
}
]
}
I'm trying to show labels on a point FeatureLayer, the label is not visible for just one Feature, But it become visible when the map change scale, is there any explanation?
Here is my code:
"renderer": {
"type": "class-breaks",
"field": "nums",
"classBreakInfos": [
{
"minValue": 0,
"maxValue": 9,
"label": " < 10",
"symbol": {
"type": "picture-marker",
"url": "./assets/picto/nums/vert.png",
"width": 30,
"height": 50
}
},
{
"minValue": 10,
"maxValue": 19,
"label": " 10 à 19",
"symbol": {
"type": "picture-marker",
"url": "./assets/picto/nums/orange.png",
"width": 30,
"height": 50
}
},
{
"minValue": 20,
"maxValue": 1000000,
"label": " > 19",
"symbol": {
"type": "picture-marker",
"url": "./assets/picto/nums/rouge.png",
"width": 30,
"height": 50
}
}
]
},
"labelingInfo": [
{
"labelExpressionInfo": {
"expression": "$feature.nums"
},
"labelPlacement": "center-center",
"symbol": {
"type": "text",
"color": "black",
"haloColor": "white",
"xoffset": -2,
"yoffset": 13,
"font": {
"size": 7,
"family": "sans-serif",
"weight": "bolder"
}
}
}
]
I joined a picture of the FeatureLayer labels problem:
Base on you definition, you are using the default deconflict strategy ("static"). Sometimes when overlaps occurs the overlapped label is not showed. Depends on the situation this strategy make sense or not.
You could try "none" option, to turn off deconfliction and you should not have that issue. It seems it might be what you are looking for.
labelClass.deconflictionStrategy = "none";
ArcGIS JS API - LabelClass deconflictionStrategy
I'm a new in October CMS. Now i have an issue with file attachment. In Model :
public $attachOne = [
'cover_image' => ['System\Models\File']
];
Now in controller :
$comic = Comic::with('cover_image)->find($id);
it return:
{
"id": 24,
"title": "Comic example detail",
"description": "Comic example detail",
"total_page": "14",
"cover_image": {
"id": 165,
"disk_name": "5d5a21ca68bf9280597931.png",
"file_name": "img06.png",
"file_size": 142958,
"content_type": "image/png",
"title": null,
"description": null,
"field": "cover_image",
"sort_order": 165,
"created_at": "2019-08-19 04:12:58",
"updated_at": "2019-08-19 04:15:23",
"path": "http://localhost:10080/storage/app/uploads/public/5d5/a21/ca6/5d5a21ca68bf9280597931.png",
"extension": "png"
}
}
I want cover_image return only some field, like below:
"cover_image": {
"id": 165,
"file_name": "img06.png",
"path": "http://localhost:10080/storage/app/uploads/public/5d5/a21/ca6/5d5a21ca68bf9280597931.png",
}
how can i do it?
use constraint eager loading
see in the doc https://octobercms.com/docs/database/relations#constraining-eager-loads
it is explained with where statement but you can use select statement and you can get only required attributes.
I am sure it will help you.
I am drawing a intractive graph using amcharts. I want to draw several line graphs in one single graph. But my code draw only one graph. When I add code for second line graph , it did not show anything due to error. How I can add second line graph in it. Here is .js file.
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"theme": "light",
"marginTop":0,
"marginRight": 80,
"dataProvider": [{
"D": "100",
"value": 10
}, {
"D": "200",
"value": 20
}, {
"D": "200",
"value": 30
}, {
"D": "400",
"value": 40
}, {
"D": "500",
"value": 50
}],
"graphs": [{
"id":"g1",
"balloonText": "[[category]]<br><b><span style='font-size:14px;'>[[value]]</span></b>",
"bullet": "round",
"bulletSize": 8,
"lineColor": "#d1655d",
"lineThickness": 2,
"negativeLineColor": "#637bb6",
"type": "smoothedLine",
"valueField": "value"
}],
"chartCursor": { /* required for zoom effect */
"cursorAlpha": 0,
"valueLineEnabled":true,
"valueLineBalloonEnabled":true,
"valueLineAlpha":0.5,
"fullWidth":true
},
/*show x axis values on graph*/
"categoryField": "D",
});
chart.addListener("rendered", zoomChart);
if(chart.zoomChart){
chart.zoomChart();
}
function zoomChart(){
chart.zoomToIndexes(Math.round(chart.dataProvider.length * 0.4), Math.round(chart.dataProvider.length * 0.55));
}
I found a solution. If you want to plot more linegraphs in one single graph. Just add extra id' in "graph": and add extra valueField inside that id. And add needed points in "dataProvider":. Similarly you can do this for more than 2 graphs by adding ids inside "graph":. And save it with .js extension.
var chart = AmCharts.makeChart("chartdiv", {
"type": "serial",
"theme": "light",
"marginTop":0,
"marginRight": 80,
"dataProvider": [{
"D": "5",
"value":0.30,
"value1":0.5,
}, {
"D": "10",
"value": 0.29,
"value1":0.27,
}, {
"D": "15",
"value": 0.28,
"value1":0.20,
}, {
"D": "20",
"value": 0.27,
"value1":0.32,
}, {
"D": "25",
"value": 0.26,
"value1":0.25,
}],
"graphs": [{
"id":"g1",
"balloonText": "[[category]]<br><b><span style='font-size:14px;'>[[value]]</span></b>",
"bullet": "round",
"bulletSize": 8,
"lineColor": "#d1655d",
"lineThickness": 2,
"negativeLineColor": "#637bb6",
"type": "smoothedLine",
"title": "redpoint",
"valueField": "value"
}, {
"id":"g2",
"balloonText": "[[category]]<br><b><span style='font-size:14px;> [value]]</span></b>",
"bullet": "round",
"bulletSize": 8,
"lineColor": "#20acd4",
"lineThickness": 2,
"negativeLineColor": "#637bb6",
"type": "smoothedLine",
"title": "bluepoint",
"valueField": "value1"
}],
"chartCursor": { /* required for zoom effect */
"cursorAlpha": 0,
"valueLineEnabled":true,
"valueLineBalloonEnabled":true,
"valueLineAlpha":0.5,
"fullWidth":true
},
/*legend show points value on top*/
"legend": {
"useGraphSettings": true,
"position": "top"
},
/*show x axis values on graph*/
"categoryField": "D",
});
chart.addListener("rendered", zoomChart);/*zoom effect*/
if(chart.zoomChart){
chart.zoomChart();
}
function zoomChart(){
chart.zoomToIndexes(Math.round(chart.dataProvider.length * 0.4), Math.round(chart.dataProvider.length * 0.55));
}
I'm really new to working with JSON in Ruby, and am having a hard time figuring out why a script I'm running is generating blank lines as a result. Let's say that I'm parsing a file where one element is not always present. I want to check if that element exists, and depending on the result name variables in a specific way. Here is what I've been trying
require 'rubygems'
require 'json'
file = File.open("/path/to/file.json", encoding: 'UTF-8')
json = file.read
data = JSON.parse(json)
if data["snapshots"][-1]["responses"][2].nil?
item = something
elseif
item = something else
end
puts item
This is not generating an error, but is just producing a blank line. I'm sure I'm doing something obvious wrong, but would appreciate any help. Thanks!
Your main problem is that you had elseif instead of elsif
Full code using the JSON you provided below:
require 'json'
json = <<EOS
{
"snapshots": [
{
"steps": 10,
"responses": [
{
"tokens": [
"Answer"
],
"questionPrompt": "Question1?"
},
{
"tokens": [
"Answer"
],
"questionPrompt": "Question2?"
},
{
"locationResponse": {
"location": {
"speed": 0,
"timestamp": "2014-04-20T17: 28: 37-0400",
"longitude": "-xx.xxxxxxx",
"latitude": "xx.xxxxxx",
"verticalAccuracy": 3,
"course": 0,
"horizontalAccuracy": 5
},
"text": "Response"
},
"questionPrompt": "Question3?"
},
{
"tokens": [
"Answer"
],
"questionPrompt": "Question4?"
}
],
"battery": 0.75,
"sectionIdentifier": "1-2014-5-7",
"audio": {
"avg": -49.84988,
"peak": -39.73056
},
"background": 0,
"date": "2014-05-07T23: 20: 57-0400",
"location": {
"speed": -1,
"placemark": {
"subAdministrativeArea": "County",
"subLocality": "CityName",
"thoroughfare": "Street",
"administrativeArea": "xx",
"subThoroughfare": "xxx",
"postalCode": "xxxxx",
"region": "<+xx.xxxxxx",
"radius": 28.13,
"country": "UnitedStates",
"locality": "CityName",
"name": "Address"
},
"timestamp": "2014-05-07T23: 20: 58-0400",
"longitude": "-xx.xxxxxxx",
"latitude": "xx.xxxxxxx",
"verticalAccuracy": 10,
"course": 0,
"horizontalAccuracy": 65
},
"dwellStatus": 0,
"weather": {
"relativeHumidity": "68%",
"visibilityKM": 16.1,
"tempC": 13.3,
"precipTodayIn": 0,
"windKPH": 0,
"latitude": 40.813984,
"windDegrees": 159,
"stationID": "xxxxxxxx",
"visibilityMi": 10,
"pressureIn": 30.2,
"pressureMb": 1023,
"feelslikeF": 55.9,
"windGustKPH": 12.4,
"longitude": -77.895775,
"feelslikeC": 13.3,
"precipTodayMetric": 0,
"tempF": 55.9,
"windDirection": "SSE",
"dewpointC": 8,
"uv": 0,
"weather": "Overcast",
"windGustMPH": 7.7,
"windMPH": 0
},
"connection": 1,
"sync": 0,
"reportImpetus": 0,
"draft": 0
}
]
}
EOS
data = JSON.parse(json)
if data["snapshots"][-1]["responses"][2].nil?
item = "was nil"
elsif # THIS WAS elseif before
item = "NOT nil"
end
puts item