Python - IPython.display 사용법, 화면에 출력하기 (이미지, 동영상, HTML, 파일 링크, 수학 수식)

반응형

IPython 라이브러리를 활용하여 화면에 출력하기

무엇이 출력되냐를 기준으로 글을 작성하였기 때문에 각각의 자세한 설명은 따로 기재하지 않았습니다.

대문자 i(아이) 이며, l(엘)로 호출시 호출이 되지 않고 에러를 출력합니다.


라이브러리 호출

# 이미지 출력
from IPython.display import Image

# 유튜브 영상 출력
from IPython.display import YouTubeVideo

# HTML 문서 출력
from IPython.display import HTML

# 파일 링크
from IPython.display import FileLink, FileLinks

# 수학 수식
from IPython.display import Math

IPython - 이미지 출력

  • 소스 코드
from IPython.display import Image

img = Image(url='https://www.python.org/static/img/python-logo.png')
img
# = display(img)

 

  • 출력 화면

 


IPython - 유튜브 동영상 출력

  • 소스 코드
from IPython.display import YouTubeVideo

# 예시를 위한 유튜브 영상, 방탄소년단 MV
# 유튜브 URL 주소 뒤의 값을 넣는다.
# https://youtu.be/gdZLi9oWNZg
YouTubeVideo('gdZLi9oWNZg')

 

  • 출력 화면


IPython - HTML 문서

from IPython.display import HTML

# html 문서에 출력할 html 소스코드 입력, 테이블 2X2 생성
html_code = """<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>"""

# html 문서로 출력
html = HTML(html_code); html

 

  • 출력 화면

 


IPython - 파일 링크

FileLink

  • 소스 코드
from IPython.display import FileLink, FileLinks

FileLink('hello_world.ipynb')

 

  • 출력 화면

 

  • 링크를 누르면 해당 파일 호출

 

FileLinks

  • 소스 코드
  • 현재 실행중인 파일의 디렉토리 목록 출력 ('.')
from IPython.display import FileLink, FileLinks

FileLinks('.')

 

출력 화면


IPython - 수학 수식

  • 소스 코드
from IPython.display import Math

Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')

 

  • 출력 화면

반응형