I have the following form, it intended to be a list of multiple selection fields. E.g. to dynamically render some checkboxes like:
fruit
apple
peach
pear
plum
vegetables
potato
carrot
turnip
etc
etc
etc
class ItemSelectForm(FlaskForm):
selected = SelectMultipleField(
"Runs",
validators=[Optional()],
widget=widgets.ListWidget(prefix_label=False),
option_widget=CheckboxInput(),
coerce=int
)
class ListItemSelectForm(FlaskForm):
name = StringField('Name', validators=[InputRequired()])
selection_lists = FieldList(FormField(ItemSelectForm))
submit = SubmitField("Submit")
I cannot work out how to dynamically generate the form. This is my current code:
fruit = [(1, "apple"), (2, "peach"), (3, "pear"), (4, "plum")]
vegetables = [(1, "potato"), (2, "carrot"), (3, "turnip")]
all_items = [fruit, vegetables]
selection_lists = []
for item_list in all_items :
selection_lists.append({"selected": item_list })
form = ListItemSelectForm(selection_lists=selection_lists)
But nothing renders...
I have also tried:
selection_lists.append({"selected": {"choices": item_list }})
No errors are given
Related
I was trying to add a new column to my existing data frame in pyspark. My data frame looks
like as follows. And I was trying with the help of this post
Pyspark: Replacing value in a column by searching a dictionary
by-searching- a-dictionary
Fruit
Orange
Orange
Apple
Banana
Apple
the code I was tring as like this
from pyspark.sql import functions as F
from itertools import chain
simple_dict = {'Orange': 'OR, 'Apple': 'AP', 'Banana': 'BN'}
mapping_expr = F.create_map([F.lit(x) for x in F.chain(*simple_dict.items())])
def addCols(data):
data = (data.withColumn('Fruit_code', mapping_expr[data['Fruit']]))
return data
Expected output:
Expected output:
Fruit Fruit_code
Orange OR
Orange OR
Apple AP
Banana BN
Apple AP
I'm getting below error: I know its because of function F. But I don't know how to fix. Can someone help me ?
FILE "/MYPROJECT/DATASETS/DERIVED/OPPORTUNITY_WON.PY", LINE 8, IN <MODULE>
MAPPING_EXPR = CREATE_MAP([LIT(X) FOR X IN CHAIN(*SIMPLE_DICT.ITEMS())])
FILE "/MYPROJECT/DATASETS/DERIVED/OPPORTUNITY_WON.PY", LINE 8, IN <LISTCOMP>
MAPPING_EXPR = CREATE_MAP([LIT(X) FOR X IN CHAIN(*SIMPLE_DICT.ITEMS())])
I have modified your code snippet to get it working.
from pyspark.sql import functions as F
from itertools import chain
simple_dict = {'Orange': 'OR', 'Apple': 'AP', 'Banana': 'BN'}
mapping_expr = F.create_map([F.lit(x) for x in chain(*simple_dict.items())])
def addCols(data):
data = (data.withColumn('Fruit_code', mapping_expr[data['Fruit']]))
return data
data = spark.createDataFrame([("Orange", ), ("Apple", ), ("Banana", ), ], ("Fruit", ))
new_data = addCols(data)
new_data.show()
Output
+------+----------+
| Fruit|Fruit_code|
+------+----------+
|Orange| OR|
| Apple| AP|
|Banana| BN|
+------+----------+
I have written this code
import scrapy
class YellowPages(scrapy.Spider):
name = 'yp'
start_urls = [
"https://www.yellowpages.com/search?search_terms=agent&geo_location_terms=Los%20Angeles%2C%20CA&page=1",
]
def parse(self, response):
agent_name = response.xpath("//a[#class='business-name']/span/text()").extract()
phone_number = response.xpath("//div[#class='phones phone primary']/text()").extract()
address = response.xpath("//div[#class='street-address']/text()").extract()
locality = response.xpath("//div[#class='locality']/text()").extract()
data = zip(agent_name, phone_number, address, locality)
for item in data:
info = {
#'page' : response.url,
'Agent name': item[0],
'Phone number': item[1],
'Address': item[2],
'Locality':item[3],
}
yield info
next_page_href = response.xpath('//a[#class= "next ajax-page"]/#href').extract()[0]
next_page = "https://www.yellowpages.com"+next_page_href
if next_page is not None:
yield scrapy.Request(response.urljoin(next_page), callback=self.parse)
But now I want to have ratings added to my CSV file. bt the rating number is written in word.
like this.
<div class="result-rating three ">
On the webpage this rating is shown by stars and the number of the total stars is written in word in the code.
I want to get that rating in number. Anyone know how will I able extract the words into numbers??
Assuming the rating is from one to five, you can maintain an array of these words (one to five) and detect them in the string.
Something like this:
word_number_mapping = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
rating_value = None
rating_text = response.css('.result-rating::attr(class)').extract()
if rating_text:
for k, v in word_number_mapping.items():
if k in rating_text:
rating_value = v + 0.5 if 'half' in rating_text else v
Hope it helps.
I have a SelectMultiple IpyWidgets.
import ipywidgets as widgets
d = widgets.SelectMultiple(
options=['Apples', 'Oranges', 'Pears',"Mango"],
#rows=10,
description='Fruits',
disabled=False
)
OP:
print (list(d.value))
['Apples', 'Mango']
Irrespective of the order I select, the order in OP is always the same as in the order the list of options. For example, even if I select Mango first and then Apple the OP is still as given.
You need a workaround to catch the click order, similar as described here:
import ipywidgets as widgets
d = widgets.SelectMultiple(
options=['Apples', 'Oranges', 'Pears',"Mango"],
description='Fruits',
disabled=False
)
foo = []
def on_change(change):
if change['type'] == 'change' and change['name'] == 'value':
for elem in change['new']:
if elem not in foo:
foo.append(elem)
for elem in foo:
if elem not in change['new']:
foo.remove(elem)
d.observe(on_change)
d
foo is just a placeholder. Now if you click 'Mango' and then 'Apple', you get:
print('values:', d.value) # values: ('Apples', 'Mango')
print('click_order:', foo) # click_order: ['Mango', 'Apples']
I'm trying to dynamically create nested map like below in code.
def people = [
[name: 'Ash', age: '21', gender: 'm'],
[name: 'Jo', age: '22', gender: 'f'],
[name: 'etc.', age: '42', gender: 'f']
]
So I can search it like below
person = people.findAll {item ->
item.gender == 'm' &&
item.age == '21'}
My problem is that whilst I can dynamically create one dimensional maps in code, I don't know how to dynamically combine maps in code to create nested map e.g. let's assume in code I have created two maps name1 and name2. How do I add them to people map so they are nested like above example?
def people = [:]
def name1 = [name:'ash', age:'21', gender:'m']
def name2 = [name:'Jo', age:'22', gender:'f']
I've searched / tried so many posts without success. Below is close, but does not work :(
people.put((),(name1))
people.put((),(name2))
In your example, people is a list of maps, not a nested map
So you can simply do:
def people = []
def name1 = [name:'ash', age:'21', gender:'m']
def name2 = [name:'Jo', age:'22', gender:'f']
Then:
people += name1
people += name2
Or define it in one line:
def people = [name1, name2]
Does anyone have a quick example of how Associations, #NS and #NV work in UniData?
I’m trying to work out associations in dictionary items but cannot get them to do anything.
For example, in a record
<1,1> = A
<1,2> = B
<2,1> = Apple
<2,2> = Banana
I created 3 dictionary items. LETTER and FRUIT, COMBO as follows
LETTER:
<1> = D
<2> = 1
<3> =
<3> = Letter
<4> = 6L
<5> = M
<6> = COMBO
FRUIT:
<1> = D
<2> = 1
<3> =
<3> = Letter
<4> = 6L
<5> = M
<6> = COMBO
COMBO:
<1> = PH
<2> = LETTER FRUIT
Doing a LIST LETTER FRUIT or LIST COMBO has no difference to when LETTER and FRUIT do not have an association declared in 6.
At this point I thought it might group multivalues together when SELECTing so I created another record as such:
<1,1> = A
<1,2> = B
<2,1> = Banana
<2,2> = Apple
Doing SELECT MyFile WITH LETTER = “A” and FRUIT = “Apple” selects both records, so that cannot be it either.
I then tried changing LETTER to be:
<1> = I
<2> = EXTRACT(#RECORD,1,#NV,1);EXTRACT(FRUIT,1,#NV,1);#1:" (":#2:")" : #NS
<3> =
<3> = Letter
<4> = 6L
<5> = M
<6> = COMBO
Hoping it that a LIST MyFile LETTER would bring back all the different letters with their associated fruit in parentheses. That didn’t work either as now LETTER only ever displayed the first Multivalue instead of all of them. For Eg:
LIST MyFile LETTER 14:05:22 26 FEB 2010 1
MyFile.... LETTER..............
RECORD2 A (Banana)1
RECORD A (Apple)1
2 records listed
The manuals don’t go any further than saying the word “association”. Is anyone able to clarify this for me?
Many times NV and NS only work when using BY-EXP in your LIST or SELECT statements. You need to use modifiers that specifically look at MultiValue and SubValues.
WHEN is one, and BY-EXP is another. There are other, but not sure what they are off the top of my head. I primarly use BY-EXP and BY-EXP-DSND.
LIST MyFile BY-EXP LETTER = "A" BY-EXP FRUIT ="Apple" LETTER FRUIT LETTER.COMBO
To bring back all the combinations, you use need to do the following:
LIST MyFile BY-EXP LETTER LETTER FRUIT LETTER.COMBO
Change the following virtual field from 'LETTER' to say 'LETTER.COMBO' or something along those lines:
<1> = I
<2> = EXTRACT(#RECORD,1,#NV,1);EXTRACT(FRUIT,1,#NV,1);#1:" (":#2:")" : #NS
<3> =
<3> = Letter
<4> = 6L
<5> = M
<6> = COMBO
Hope that helps.
-Nathan
To answer part of my own question:
Only 'WHEN' is affected by the association, not with. If you turn on UDT.OPTIONS 94 and do
LIST MyFile WHEN LETTER = "A" AND FRUIT="Apple" COMBO
when using my D-Type definition of LETTER, I get
LIST MyFile WHEN LETTER = "A" AND FRUIT="Apple" LETTER FRUIT 16:06:42 26 FEB 2010 1
MyFile.... LETTER.............. FRUIT...............
RECORD A Apple
1 record listed
Which is what one would expect.
To use the WHEN clause you need to be in ECLTYPE U, not P. IT would be helpful if this was clearer, but oh well...