Categories
Python Answers

How to set the background color for react-select drop downs?

Sometimes, we want to set the background color for react-select drop downs.

In this article, we’ll look at how to set the background color for react-select drop downs.

How to set the background color for react-select drop downs?

To set the background color for react-select drop downs, we can return an object with the color values set.

For instance, we write:

import React from "react";
import Select from "react-select";

const customStyles = {
  control: (base, state) => ({
    ...base,
    background: "#023950",
    borderRadius: state.isFocused ? "3px 3px 0 0" : 3,
    borderColor: state.isFocused ? "yellow" : "green",
    boxShadow: state.isFocused ? null : null,
    "&:hover": {
      borderColor: state.isFocused ? "red" : "blue"
    }
  }),
  menu: (base) => ({
    ...base,
    borderRadius: 0,
    marginTop: 0
  }),
  menuList: (base) => ({
    ...base,
    padding: 0
  })
};

const options = [
  { label: "Apple", value: "apple" },
  { label: "Orange", value: "orange" }
];

export default function App() {
  return (
    <form>
      <Select styles={customStyles} options={options} />
    </form>
  );
}

We set the styles prop to the customStyles object which has various styles.

The control method in the object returns an object with the style values.

The properties returned includes background, borderRadius, borderColor, boxShadow and other CSS style properties.

We can also style states like hover with "&:hover".

And we can get the state of the drop down from the state parameter.

Likewise, we have the menu and menuList methods to style the menu.

We set the options prop to an array of options.

Now we see we the drop down has a dark blue background and the drop down row that’s hovered over has a light blue background.

Conclusion

To set the background color for react-select drop downs, we can return an object with the color values set.

Categories
Python Answers

How to merge several Python dictionaries?

Sometimes, we want to merge several Python dictionaries.

In this article, we’ll look at how to merge several Python dictionaries.

How to merge several Python dictionaries?

To merge several Python dictionaries, we can use the ** operator to unpack dictionary entries into another dictionary.

For instance, we write:

a = {'a': 1, 'b': 2, 'c': 3}
b = {'d': 1, 'e': 2, 'f': 3}
c = {1: 1, 2: 2, 3: 3}
merge = {**a, **b, **c}
print(merge)

to merge the entries of a, b, and c into the merge dictionary by unpacking the entries from a, b, and c with the ** operator.

Therefore, merge is {'a': 1, 'b': 2, 'c': 3, 'd': 1, 'e': 2, 'f': 3, 1: 1, 2: 2, 3: 3}.

Conclusion

To merge several Python dictionaries, we can use the ** operator to unpack dictionary entries into another dictionary.

Categories
Python Answers

How to do locale date formatting in Python?

Sometimes, we want to do locale date formatting in Python.

In this article, we’ll look at how to do locale date formatting in Python.

How to do locale date formatting in Python?

To do locale date formatting in Python, we can use the locale and datetime modules

For instance, we write:

import locale
import datetime

locale.setlocale(locale.LC_TIME, '')
date_format = locale.nl_langinfo(locale.D_FMT)
d = datetime.date(2020, 4, 23)
print(d.strftime(date_format))

We call locale.setlocale to set the locale of the script.

Then we get the date format with:

date_format = locale.nl_langinfo(locale.D_FMT)

Finally, we create the date with datetime.date and format it into a date string with strftime with date_format as its argument.

Therefore, we see '04/23/2020' printed.

Conclusion

To do locale date formatting in Python, we can use the locale and datetime modules

Categories
Python Answers

How to get intersecting rows across two 2D Python NumPy arrays?

Sometimes, we want to get intersecting rows across two 2D Python NumPy arrays.

In this article, we’ll look at how to get intersecting rows across two 2D Python NumPy arrays.

How to get intersecting rows across two 2D Python NumPy arrays?

To get intersecting rows across two 2D Python NumPy arrays, we can convert the arrays to sets and then use the & operator to get the intersection of both sets.

For instance, we write:

import numpy as np

A = np.array([[1, 4], [2, 5], [3, 6]])
B = np.array([[1, 4], [3, 6], [7, 8]])
aset = set([tuple(x) for x in A])
bset = set([tuple(x) for x in B])
intersection = np.array([x for x in aset & bset])
print(intersection)

We have 2 arrays of lists A and B that we created with np.array.

Then we convert both arrays to sets with set.

And we convert each entry in A and B to tuples with tuple.

Next, we get the common entries from each set with [x for x in aset & bset] and put them in a list.

Finally, we convert the list back to an array with np.array.

Therefore, intersection is:

[[1 4]
 [3 6]]

Conclusion

To get intersecting rows across two 2D Python NumPy arrays, we can convert the arrays to sets and then use the & operator to get the intersection of both sets.

Categories
Python Answers

How to initialize a dictionary of empty lists in Python?

Sometimes, we want to initialize a dictionary of empty lists in Python.

In this article, we’ll look at how to initialize a dictionary of empty lists in Python.

How to initialize a dictionary of empty lists in Python?

To initialize a dictionary of empty lists in Python, we can use dictionary comprehension.

For instance, we write:

data = {k: [] for k in range(2)}
print(data)

to create a dictionary with 2 entries that are both set to empty lists as values.

Therefore, data is {0: [], 1: []}.

Conclusion

To initialize a dictionary of empty lists in Python, we can use dictionary comprehension.