Friday, January 17, 2020

Using Vue Variable in Jinja Python Flask


- Using {% raw %} directive
ex.

<div id="app">
{% raw %}
{{ var_from_vue }}
{% endraw %}
</div>

- Using Vue delemiters in Vue Instant
ex.

var app = new Vue({
    el: '#app',
    data: {
        v1: 'variable 1'
    },
    delimiters: ['[[', ']]'] 
})

- Custom Directive in Python Flask Jinja
ex.

app = Flask(__name__)

jinja_options = app.jinja_options.copy()
jinja.options.update(dict(
    variable_start_string='[[',
    variable_end_string=']]'
))
app.jinja_options = jinja_options

Tuesday, December 3, 2019

How to Caching Post Requests to redis Server in Python Flask, Redis Using Decorators

from flask import Flask, jsonify, request
from functools import wraps
import redis
import json

r = redis.Redis('__ip_address_redis_server__')
cache_time = 10

def xcaching(f):
    @wraps(f)
    def decor(*args, **kwargs):
        check = request.get_json()
        get_from_redis = r.get(str(check))

        if get_from_redis:
            data = get_from_redis.decode('utf-8')
            ret = json.loads(data)
            r.expire(str(check), cache_time)
            return jsonify(ret)

        ret = f(*args, **kwargs)

        cache_to_redis = ret.get_data()
        r.set(str(check), cache_to_redis ex=cache_time)

        return ret
    return decor


@app.route('/search', methods=['POST'])
@xcaching
def main():
req = request.get_json()
ret = call_api_function(req)
return jsonify(ret)

'__repr__' example

class TestClass:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def __repr__(self):
        return '{self.__class__.__name__}({self.a}, {self.b})'.format(self=self)

Sunday, December 1, 2019

Convert dictionary 'key' in list to lowercase (python3)

Using List Comprehension and Dictionary Comprehension
>>> rs = [{'ABC': '123', 'DEF': '456'}]
>>> ret = [{k.lower():v for k,v in i.items()} for i in rs]
>>> ret
>>> [{'abc': '123', 'def': '456'}]

Using Lambda Function
>>> rs = list(map((lambda i: { k.lower(): v for k,v in i.items()}), rs))

Friday, November 22, 2019

How to set TimeZone in Linux Docker Container

#How to set TimeZone in Linux Docker Container

```
apk add tzdata
```
ex:
Dockerfile
```
from python:3.7-alpine
RUN apk add tzdata
ENV TZ=Asia/Bangkok
CMD xxxx
```

#TimeZone in slim and Ubuntu Linux Docker Container

ex:
Dockerfile
```
from python
ENV TZ=Asia/Bangkok
CMD xxxx
```

ex:
Run Container (slim or full linux images)
```
docker run --name container-name -e 'TZ=Asia/Bangkok' python
```

Wednesday, November 20, 2019

SET TimeZone on Ubuntu19 with Command Line

:Find Current TimeZone
$ timedatectl list-timezones | grep -i [zone]

ex:
$ timedatectl list-timezones | grep -i europe

:Remove link
$ sudo unlink /etc/localtime

:Set new TimeZone
sudo ln -s /usr/share/zoneinfo/[Zone/TimeZone] /etc/localtime

ex: 
sudo ln -s /usr/share/zoneinfo/Asia/Bangkok /etc/localtime

ALCATEL 6900

write memory copy running certified reload from working no rollback-timeout