티스토리 블로그에서도 코드블럭을 사용하여 간단한 코드를 공유할 수 있지만, Jupyter notebook file(.ipynb)은 공유하기 어려운 단점이 있다. 이때 GitHub Gist를 활용하면 Jupyter notebook file을 깔끔한 형식으로 공유할 수 있어서 코드 뿐 아니라 실행 결과 등을 한번에 확인할 수 있다는 장점이 있다.
GitHub Gist를 활용하는 방법은 굉장히 간단하다. 먼저 GitHub에 들어가서 [New gist]를 클릭한다.

그러면 아래와 같은 창이 나온다.

[Gist description]을 통해 코드 설명을 적을 수 있고, [Add file]을 통해 여러 개의 파일을 한번에 올릴 수도 있다. Jupyter notebook을 작성한 후 해당 파일(220226_first_gist.ipynb)을 복사해서 붙여넣었다. 220226_first_gist.txt는 테스트를 위해 txt 포맷으로 파일을 추가하였다.

[Create secret gist]를 누르면 URL이 있는 사용자만 해당 gist를 볼 수 있다. 자신의 피드에 gist를 공개하고 싶다면 [Create public gist]를 누른다.
Gist가 생성되면 아래의 코드를 복사한다.
<script src="https://gist.github.com/Bioinfoblog/fc294a6e5b7eb8f67fa766248f4d2fef.js"></script>

티스토리 블로그에서 기본모드 대신 HTML을 선택한다.

원하는 위치에 해당 코드를 붙여넣기하면 된다.

결과물 🙆♂️
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "5fff7114-21d2-4e3d-9479-6dc05a6b03ac", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hello World!\n" | |
] | |
} | |
], | |
"source": [ | |
"# test code\n", | |
"print(\"Hello World!\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "6f0d0ced-294a-4126-a0e2-43eeaf87f915", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hello World!\n" | |
] | |
} | |
], | |
"source": [ | |
"# test code\n", | |
"print(\"Hello World!\")" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.9.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
굉장히 깔끔한 형식으로 공유되는 모습을 확인할 수 있다. 또한 파일 확장자에 따라 (ipynb, txt 등) 다른 형식으로 코드를 보여주었다.
참고
다음의 사이트에서 여러 사용자들의 gist를 확인할 수 있다.
https://gist.github.com/discover
Discover gists
GitHub Gist: instantly share code, notes, and snippets.
gist.github.com
'Computer Science > Git & GitHub' 카테고리의 다른 글
[GitHub] 폴더 내 코드를 GitHub에 백업하는 방법 (0) | 2022.03.19 |
---|---|
[GitHub] 개발을 위한 Branch 관리 (Upstream, Origin, Local repository) (0) | 2021.03.26 |