2021年6月13日 星期日

VS code 開發必裝套件

 HTML :

即時顯示開發結果

Live server

Icon

幫助我們把code上色,以清楚辨識

Material Icon Theme

Monokai++

React: 

Simple React snippet 



開發方便:

Auto-Save on Window Change

2021年2月18日 星期四

Docker mysql cheat sheet

從備份檔還原資料庫

建資料庫
docker run --name <你想要的容器名> -p <TCP 可用的port:容器內的port,例如33060:3306> -e MYSQL_ROOT_PASSWORD=<你想要的密碼> -d mysql:latest

 把備份檔放進容器
docker cp <路徑/備份檔案名稱.sql> <容器ID>:/<容器內路徑>

進去容器
mysql -uroot -p
> CREATE DATABASE <DB Name>
> exit

mysql -uroot -p <DB Name> < <backup file name.sql>

即可

備份資料庫
進去docker
執行
mysqldump --all-databases --single-transaction --quick --lock-tables=false > full-backup-$(date +%F).sql -u root -p
出來到本機的cmd
docker cp <容器ID>:/<容器內路徑, e.g../home/data.sql>
<路徑/備份檔案名稱.sql eg, D:/backup.sql>

2020年12月28日 星期一

轉址怎麼設定

常常我們萬不得已需要轉址,讓某些處在網路限制區域的捧油,可以訪問我們的服務。

這個時候就相當麻煩,因為大多數的捧油,是把static資料夾,用collectstatic的方式,讓例如nginx或是apache來處理。這樣轉址只要另外設定轉址位置即可。

若是development環境,則是這樣設置:
假設我們轉址網址是
http://foo/bar/
STATIC_URL='/bar/static/'
MEDIA_URL='/bar/media/'
LOGIN_REDIRECT_URL='/bar/bar/'
LOGIN_URL='bar/login/'

然後記得URLs.py設定
path('bar/', include('mysite.urls'))
即可。

2020年12月2日 星期三

Pandas 容易踩的坑(持續更新)

最近都在弄這隻熊貓,發現其中的坑還不少,趕快紀錄下來,希望大家不要跟我一樣了。

1. Python針對list的append,是立刻發生的,而Pandas dataframe 的append不是。
In Python:
foo=[]
bar=1
foo.append(bar)
print(foo)
....[1]

In Pandas:
foo = pd.DataFrame([[1,2]], columns=['A','B'])
bar = pd.DataFrame([[2,2]], columns=['A','B'])
foo.append(bar)
print(foo)
...A. B
0 1 2

所以要記得把結果存起來才行
foo = pd.DataFrame([[1,2]], columns=['A','B'])
bar = pd.DataFrame([[2,2]], columns=['A','B'])
foo=foo.append(bar)
print(foo)
...A. B
0 1 2
1 2 2



2020年11月2日 星期一

Docker MySQL cheat list

先移到打算存備份檔案的資料夾裡面

再執行

docker cp <container id>:<path inside container, eg. /home/mysql.sql> .

這樣子就已經把備份檔存到本機目前所在資料夾裡面了。

之後,再把檔案移到新的容器裡

docker exec -i <container id> sh -c 'cat > <path you want to store the data, eg. /home/mysql.sql' < <備份檔案名, e.g. mysql. sql>

再來要進去那個容器

mysql -uroot -p <database name you backed up> < <path to the back up file, eg. /home/my first.sql >

登進去看看

mysql -uroot -p

>SHOW DATABASES; 

就完成囉!


2020年10月23日 星期五

csv的大坑

最近在弄產生csv檔給網頁使用者下載,才發現原來csv在處理中文的坑這麼多。趕快寫下來,不要重蹈覆轍了。

目標:讓使用者下載csv,且可以直接用Excel編輯,而不出現亂碼。


首先,把資料從資料庫裡面撈出來的時候,格式通常不會有問題。
所以只要
data = Data.objects.filter(name__icontains=Liang)
data_list = []
for i in data:
   data_list.append(i.name)
data_list.append(一些中文)
在寫入的時候,要使用utf_8_sig
with open('foo.csv', 'w', newline='', encoding='utf_8_sig') as f:
    writer  = csv.writer(f, delimiter=',')
    writer.writerow(data_list)

而提供檔案的時候:
def fake_view(request):
    file = open('foo.csv', 'r', encoding='utf-8')
    response = HttpResponse(file)
    response ['Content-Type'] ='application/octet-stream'
    response['Content-Disposition'] = 'attachment; filename="<The file name you want the user to have>" '
    return response 

2020年10月1日 星期四

Json常用指令

 當跟數據科學家合作案件,常常是用json傳遞資料。我們如果從資料庫拉資料出來,也常常是dictionary格式。

這裡梁哥整理一下平常常用的指令,作為一個cheat sheet。

假設出現json.decoder.JSONDecodeError:  Expecting property name enclosed in double quotes

你就需要將單引號換成雙引號,

str = str.replace("  \' ", "  \"  ")


如果需要轉成dictionary,就要搭配json.loads()

dictionary = json.loads(str)