1

I would like to run dtale without first opening a python shell, so I would like to run it with a script like this:

python data_analysis_dtale.py

where the content of data_analysis_dtale.py is:

import pandas as pd
import dtale

df = pd.DataFrame({'a':[1,2,3], 'b':[3,4,5]})

s = dtale.show(df)
s.open_browser()

If I run this, I end up with an empty html page opened in the browser. Instead, if I execute this code during a python interactive shell session, it works fine. Is there a way to avoid this? I suppose this is a more general problem and it's not related only to dtale.

Dude
  • 11
  • 2

2 Answers2

3

You cam embed it inside flask app which can be run through python script. You can see that dtale instance is created after navigating to the route /create-df:

from flask import redirect
import pandas as pd
from dtale.app import build_app
from dtale.views import startup

if __name__ == '__main__':
    app = build_app(reaper_on=False)

    @app.route("/create-df")
    def create_df():
        df = pd.DataFrame(dict(a=[1, 2, 3], b=[4, 5, 6]))
        instance = startup(data=df, ignore_duplicate=True)
        return redirect(f"/dtale/main/{instance._data_id}", code=302)

    @app.route("/")
    def hello_world():
        return 'Hi there, load data using <a href="/create-df">create-df</a>'

    app.run(host="0.0.0.0", port=8080)

source: https://github.com/man-group/dtale/blob/master/docs/EMBEDDED_FLASK.md

0

Micho's answer is a great example of how to extend D-Tale and create your own Flask application. The easiest way to run D-Tale as a script is to use the "subprocess" parameter. For example

import dtale
import pandas as pd

if __name__ == "__main__":
    df = pd.DataFrame([1,2,3,4,5])
    dtale.show(df, subprocess=False)

This will allow D-Tale to not run as a subprocess which is how standard scripted Flask applications run.

Dharman
  • 30,962
  • 25
  • 85
  • 135