Schema Length Validation Error on Python Dash DataTable - dropdown

I am building a dashboard using python dash and have a requirement of datatable with multiple dropdowns. The dataframe is created one with groupby (multiple columns) and aggregation.
I have written the code as below:
'''
app = JupyterDash()
app.layout = html.Div(children = [html.H2('SPEND PLANNING',
style = {'background-color':'#C0C0C0',
'border': '5px solid',
'font-family':'Serif',
'font-size': 25,
'text-align':'center',
'margin':'auto',
'justify-content': 'space-evenly',
'width':'100%'
}),
html.H1(),
html.Div(children = [dcc.Dropdown(
id ='period-filter',
options = dict([{"label": i, "value": i} for i in s2['PERIOD'].unique()]),
value = s2['PERIOD'].max(),
multi = True,
searchable=True,
style = {'display':'inline-block','width':'48%','vertical-align':'top'},
clearable=False,
placeholder="Quarter"
)]),
html.H1(),
html.Div(children = [dcc.Dropdown(
id ='commodity-filter',
options = dict([{"label": i, "value": i} for i in s2['COMMODITY GROUP'].unique()]),
value = s2['COMMODITY GROUP'].max(),
multi = True,
searchable=True,
style = {'display':'inline-block','width':'48%'},
clearable=False,
placeholder= "Commodity Group"
)]),
html.H1(),
html.Div(children = [dt(
id = "datatable",
columns = [{"name": i, "id": i} for i in s2.columns],
editable = True,
data = s2.to_dict('records'),
multi = True
])
#app.callback(output = [Output("datatable", "data")],
inputs = [Input("period-filter", "value"),Input("commodity-filter","value")])
def display_table(comm,period):
filtered_df = s2.loc[(s2['COMMODITY GROUP'].eq('comm')) & (s2['PERIOD'].eq('period'))]
return filtered_df.to_dict('records')
if _name_ == "_main_":
app.run_server(debug=True)
'''
The error after running the script is below:
SchemaLengthValidationError: Schema: [<Output datatable.data>]
Path: ()
Expected length: 1
Received value of length 0:
[]
When I remove the [] from [Output("datatable", "data")] the error is gone, however the data in the table also disappears giving a blank table.
The dropdowns also do not show the values, instead shows only - value.
Please help me with the above.

Related

how to count a value in a list based on the same value in another list. and the both are dynamic when i get the response in karate

list 1 ["ART - Run and Support","ART - Run and Support","Clt group","Clt group"]
list 2 ["ART - Run and Support","Clt group",]
I want to take the 1st value in list2 and count the occurrences in list 1.
"ART - Run and Support" = 2
I was able to take for 1 value, not sure how to pass this in loop. Please do help.
* def condition = function(x){ return x == "Application Development" }
* def output = karate.filter(Total_List_AssignmentGroup, condition).length
* print output
I was able to take for 1 value, not sure how to pass this in loop. Please do help.
This was a fun problem ! Here is the solution, and believe it or not it is just one line:
* def list1 = ["ART - Run and Support", "ART - Run and Support", "Clt group", "Clt group"]
* def list2 = ["ART - Run and Support", "Clt group"]
* def results = list2.map(x => ({ name: x, count: list1.filter(y => x === y).length }))
* print results
Which gives you:
[
{
"name": "ART - Run and Support",
"count": 2
},
{
"name": "Clt group",
"count": 2
}
]
This is just using JavaScript array operations such as map() and filter()

MongoDB delete data using regex

I was able to use the following to delete data using pandas:
import re
repl = {r'<[^>]+>': '',
r'\r\n': ' ',
r'Share to facebook|Share to twitter|Share to linkedin|Share on Facebook|Share on Twitter|Share on Messenger|Share on Whatsapp': ''}
articles['content'] = articles['content'].replace(repl, regex=True)
How can I do the same on the actual database thats in Atlas?
My data structure is:
_id:
title:
url:
description:
author:
publishedAt:
content:
source_id:
urlToImage:
summarization:
MongoDB does not have any built-in Operators to perform Regex Replace on the go (for now).
You can loop through the documents using the regex find in the programming language of your choice and replace that way instead.
from pymongo import MongoClient
import re
m_client = MongoClient("<MONGODB-URI-STRING")
db = m_client["<DB-NAME>"]
collection = db["<COLLECTION-NAME>"]
replace_dictionary = {
r'<[^>]+>': '',
r'\r\n': ' ',
r'Share to facebook|Share to twitter|Share to linkedin|Share on Facebook|Share on Twitter|Share on Messenger|Share on Whatsapp': ''
}
count = 0
for it in collection.find({
# Merge all refex finds to a single list
"$or": [{"content": re.compile(x, re.IGNORECASE)} for x in replace_dictionary.keys()]
}, {
# Project only the field to be replaced for faster execution of script
"content": 1
}):
# Iterate over regex and replacements and apply the same using `re.sub`
for k, v in replace_dictionary.items():
it["content"] = re.sub(
pattern=k,
repl=v,
string=it["content"],
)
# Update the regex replaced string
collection.update_one({
"_id": it["_id"]
}, {
"$set": {
"content": it['content']
}
})
# Count to keep track of completion
count += 1
print("\r", count, end='')
print("DONE!!!")

Problems with RODBC sqlSave, Error in odbcUpdate

I am trying to insert my data into my SQL using RODBC
This is the code I am using:
sqlSave(
odbcChannel,
final,
tablename = "SRP_KAZ_Mega_Shipment_Plan",
rownames = FALSE,
colnames = FALSE,
safer = TRUE,
fast = TRUE,
append = TRUE,
varTypes = c(
филиал = "nvarchar(250)",
SKU = "nvarchar(250)",
Month = "datetime",
Volume = "float",
Feature = "nvarchar(250)",
Period = "nvarchar(250)"
)
)
And this is what I get:
Error in odbcUpdate(channel, query, mydata, coldata[m, ], test = test, :
missing columns in 'data'
I don't know what is wrong here, but whenever I run the exact same code and data on other computers it works perfectly fine.
Any ideas of what this might be will do.
Thanks

RStudio shiny datatables save csv unquoted?

How can I save the output of an RStudio Shiny DataTables table using the Save to CSV extension, but have the content saved unquoted instead of the default, which is in double-quotes:
For example, for a single column with two entries, I get a file.csv like this:
"column_name"
"foo"
"bar"
And instead I would like either:
column_name
foo
bar
Or even better, without the header:
foo
bar
My current code looks like this:
output$mytable <- renderDataTable({
entries()
}, options = list(colnames = NULL, bPaginate = FALSE,
"sDom" = 'RMDT<"cvclear"C><"clear">lfrtip',
"oTableTools" = list(
"sSwfPath" = "copy_csv_xls.swf",
"aButtons" = list(
"copy",
"print",
list("sExtends" = "collection",
"sButtonText" = "Save",
"aButtons" = list("csv","xls")
)
)
)
)
)
EDIT:
I tried with one of the suggested answers, and ajax is not allowed, the page complains when I click on SaveTXT. If I do the following, it still puts things within double quotes:
list("sExtends" = "collection",
"sButtonText" = "SaveTXT",
"sFieldBoundary" = '',
"aButtons" = list("csv")
Any ideas?
It should be possible through button options:Button options
And changing sFieldBoundary value.
$(document).ready( function () {
$('#example').dataTable( {
"sDom": 'T<"clear">lfrtip',
"oTableTools": {
"aButtons": [
{
"sExtends": "ajax",
"sFieldBoundary": '"'
}
]
}
} );
} );
But I couldn't get it working in shiny.

Create a combined bar/line chart with dimplejs and use self-defined colors

I'm trying to create a combined bar/line chart based on a simple data set (columns: countries, index1, index2, index3, ...) using dimplejs. Index1 will be the bar chart and index2 upwards should be dynamically (=adding and removing indices per user interaction) displayed as line charts on top.
I found out that I seemingly needed to create a hidden 2nd x-axis for the line graphics. Is there any other way?
I was also unable to add more than one index as line chart. Is there a way to add more lines where each one is based on a column (index2, index3, ...)?
Defining colors was another problem: The bars are all in one color which is good but if I want to assign a color myself I have to give the data a tag which is displayed in the tooltip (not good)?
Assigning colors to the line charts didn't work at all for me. I would need some advice here.
My code so far:
var svg = dimple.newSvg("#graphic", 550, 700);
// Get data for one year
var filteredData = dimple.filterData(data, 'year', '2010');
var myChart = new dimple.chart(svg, filteredData);
myChart.setBounds(50, 30, 480, 630);
var xAxis = myChart.addMeasureAxis('x', 'index1');
xAxis.title = 'Index Value';
xAxis.overrideMax = 1;
var yAxis = myChart.addCategoryAxis('y', 'countries');
yAxis.title = 'Country';
var xAxis2 = myChart.addMeasureAxis('x', 'index2');
xAxis2.title = null;
xAxis2.overrideMax = 1;
xAxis2.hidden = true;
var bars = myChart.addSeries('bars', dimple.plot.bar, [xAxis,yAxis]);
myChart.assignColor('bars', '#D3D3D3', '#D3D3D3');
var lines = myChart.addSeries('index2', dimple.plot.line, [xAxis2,yAxis]);
yAxis.addOrderRule('index1', false);
myChart.draw();
That data structure is not ideal for your data requirement within dimple. The way dimple would like that data is with your index names as dimension values:
var data = [
{ "Index" : "Index 1", "Country" : "UK", "Index Value": 0.2 },
{ "Index" : "Index 1", "Country" : "Spain", "Index Value": 0.7 },
{ "Index" : "Index 2", "Country" : "UK", "Index Value": 0.5 },
{ "Index" : "Index 2", "Country" : "Spain", "Index Value": 0.6 },
{ "Index" : "Index 3", "Country" : "UK", "Index Value": 0.4 },
{ "Index" : "Index 3", "Country" : "Spain", "Index Value": 0.3 },
...
];
Then in order to draw index 1 as a bar and the rest as lines you would need to split your data set into 2:
var barData = [],
lineData = [],
i,
keyIndex = "Index 1";
for (i = 0; i < data.length; i += 1) {
if (data[i]["Index"] === keyIndex) {
barData.push(data[i]);
} else {
lineData.push(data[i]);
}
}
You could then just define your chart as follows:
var chart = new dimple.chart(svg),
bars,
lines;
chart.addMeasureAxis("x", "Index Value");
chart.addCategoryAxis("y", "Country");
bars = chart.addSeries("Index", dimple.plot.bar);
bars.data = barData;
lines = chart.addSeries("Index", dimple.plot.line);
lines.data = lineData;
chart.draw();
I haven't tested this code but it ought to do what you want (minus the formatting etc).
If you want to continue on the road you have started in your code above (which is still possible) you will find composite axes very helpful to avoid hidden secondary axes and potential problems with differing max/min values. See the relevant section in the version 2 release notes for an example of these.
I don't understand why you don't want to tag the indices with something which appears in the tooltip, there must be some difference between them which you can communicate, but if you want to remove the index name from the tooltip you can define custom tooltips as shown here.
Edit: I should add if you just want to change the set of colours which dimple will arbitrarily ascribe to your data points you can override the default colours in the chart object as discussed here.