Categories
Python Answers

How to execute raw SQL in Flask-SQLAlchemy app with Python?

Sometimes, we want to execute raw SQL in Flask-SQLAlchemy app with Python.

In this article, we’ll look at how to execute raw SQL in Flask-SQLAlchemy app with Python.

How to execute raw SQL in Flask-SQLAlchemy app with Python?

To execute raw SQL in Flask-SQLAlchemy app with Python, we can use the db.session.execute method.

For instance, we write

result = db.session.execute('SELECT * FROM my_table WHERE my_column = :val', {'val': 5})

to call db.session.execute with the SQL string and dict with the values to fill for the placeholders.

We get the result returned from the select statement returned with execute.

Conclusion

To execute raw SQL in Flask-SQLAlchemy app with Python, we can use the db.session.execute method.

Categories
Python Answers

How to fix the number of digits after decimal with f-strings with Python?

Sometimes, we want to fix the number of digits after decimal with f-strings with Python.

In this article, we’ll look at how to fix the number of digits after decimal with f-strings with Python.

How to fix the number of digits after decimal with f-strings with Python?

To fix the number of digits after decimal with f-strings with Python, we can use format specifiers.

For instance, we write

s = f'{value:{width}.{precision}}'

to set the width and precision to format the value float with.

width is the number of characters total to display.

precision is the number of characters after the decimal point.

Conclusion

To fix the number of digits after decimal with f-strings with Python, we can use format specifiers.

Categories
Python Answers

How to get a value from a cell of a dataframe with Python Pandas?

Sometimes, we want to get a value from a cell of a dataframe with Python Pandas.

In this article, we’ll look at how to get a value from a cell of a dataframe with Python Pandas.

How to get a value from a cell of a dataframe with Python Pandas?

To get a value from a cell of a dataframe with Python Pandas, we can use the loc method.

For instance, we write

df1.loc['a', 'A'] 

to get the value at index a with column A.

Conclusion

To get a value from a cell of a dataframe with Python Pandas, we can use the loc method.

Categories
Python Answers

How to run function from the command line with Python?

Sometimes, we want to run function from the command line with Python.

In this article, we’ll look at how to run function from the command line with Python.

How to run function from the command line with Python?

To run function from the command line with Python, we can run python with the -c flag.

For instance, we run

$ python -c 'import foo; print(foo.hello())'

to run python -c with a string with the code we want to run.

Conclusion

To run function from the command line with Python, we can run python with the -c flag.

Categories
Python Answers

How to parse boolean values with argparse in Python?

Sometim,es, we want to parse boolean values with argparse in Python.

In this article, we’ll look at how to parse boolean values with argparse in Python.

How to parse boolean values with argparse in Python?

To parse boolean values with argparse in Python, we can call add_argument with the action argument.

For instance, we write

parser.add_argument('--feature', action=argparse.BooleanOptionalAction)

to parse the --feature argument as a boolean by setting action to argparse.BooleanOptionalAction.

This works with Python 3.9 or later.

With older versions of Python, we write

parser.add_argument('--feature', action='store_true')

to call add_argument to parse the --feature argument as a boolean by setting action to 'store_true'.

Conclusion

To parse boolean values with argparse in Python, we can call add_argument with the action argument.