Search code examples
google-colaboratory

Open plaintext files like python files


If you tinker a lot with Google Colab maybe you want to edit some .txt files or view a .csv file or any other kind of .csv files, you can already open python files. If you go to the file directory listing and click

enter image description here

Just click on the file you want to open, and you open a new tab

enter image description here

However if this file is not a python file, you simply download it, is there a way to view the file in browser?


Solution

  • I made a library to show files easily in Colab.

    !pip install kora
    

    For any text file, you can use bat which provide syntax highlight. See documentation

    import kora.install.bat
    !bat sample_data/README.md
    

    For a csv, you can use google.colab.data_table, which I simplify calling it a bit.

    import pandas as pd
    import kora.data_table
    pd.read_csv('sample_data/california_housing_test.csv')
    

    For a json file, it can make a collapsible display

    from kora.json import render
    render('sample_data/anscombe.json')
    

    See all 3 examples in this notebook.