def cli(db_path, tableau_views): """Fetch data from Tableau into a SQLite database Pass this command a SQLite databsae file and one or more Tableau views, where a Tableau view looks like this: OregonCOVID-19VaccineProviderEnrollment/COVID-19VaccineProviderEnrollment For example: tableau-to-sqlite tableau.db OregonCOVID-19VaccineProviderEnrollment/COVID-19VaccineProviderEnrollment You can also pass a full URL to a Tableau dashboard, for example: tableau-to-sqlite tableau.db https://results.mo.gov/t/COVID19/views/VaccinationsDashboard/Vaccinations """ for view in tableau_views: if not (view.startswith("http://") or view.startswith("https://")): url = "https://public.tableau.com/views/{}".format(view) else: url = view ts = TableauScraper() ts.loads(url) dashboard = ts.getDashboard() conn = sqlite3.connect(str(db_path)) for worksheet in dashboard.worksheets: worksheet.data.to_sql(fix_name(worksheet.name), conn)
def test_TableauScraper_getWorksheets(mocker: MockerFixture) -> None: mocker.patch("tableauscraper.api.getTableauViz", return_value=tableauVizHtmlResponse) mocker.patch("tableauscraper.api.getTableauData", return_value=tableauDataResponse) ts = TS() ts.loads(fakeUri) dashboard = ts.getDashboard() assert len(dashboard.worksheets) == 2 assert dashboard.worksheets[0].name == "[WORKSHEET1]" assert dashboard.worksheets[1].name == "[WORKSHEET2]"
def request_tableau_scraper(query): ts = TableauScraper() ts.loads(query.url) dashboard = ts.getDashboard() dfs = [] prefixes = [] if not query.params else query.params.get('worksheet', []) for ws in dashboard.worksheets: if prefixes is None: dfs.append(ws.data) elif any([ws.name.startswith(n) for n in prefixes]): dfs.append(ws.data) return dfs
from tableauscraper import TableauScraper as TS url = "https://public.tableau.com/views/PlayerStats-Top5Leagues20192020/OnePlayerSummary" ts = TS() ts.loads(url) dashboard = ts.getDashboard() # show dropdown input name inputNames = dashboard.getDropdownInputs() print(inputNames) # show dropdown values for a given input name values = dashboard.getDropdownValues("P.League 2") print(values) # select that value dashboard = dashboard.setDropdown("P.League 2", "Ligue 1") # display worksheets for t in dashboard.worksheets: print(t.data)
def test_TableauDashboard(mocker: MockerFixture) -> None: mocker.patch( "tableauscraper.api.getTableauViz", return_value=tableauVizHtmlResponse ) mocker.patch("tableauscraper.api.getTableauData", return_value=tableauDataResponse) mocker.patch("tableauscraper.api.select", return_value=vqlCmdResponse) ts = TS() ts.loads(fakeUri) dataFrameGroup = ts.getDashboard() assert type(dataFrameGroup) is TableauDashboard assert "_originalData" in dataFrameGroup.__dict__ assert dataFrameGroup.__dict__["_scraper"] is ts assert not dataFrameGroup.cmdResponse assert len(dataFrameGroup.worksheets) == 2 # get worksheet names (initial response) worksheetNames = dataFrameGroup.getWorksheetNames() assert type(worksheetNames) is list assert not dataFrameGroup.cmdResponse assert worksheetNames == ["[WORKSHEET1]", "[WORKSHEET2]"] # get worksheet names (vql response) dataFrameGroup = dataFrameGroup.worksheets[0].select("[FIELD1]", "2") assert dataFrameGroup.cmdResponse assert len(dataFrameGroup.worksheets) == 1 worksheetNames = dataFrameGroup.getWorksheetNames() assert type(worksheetNames) is list assert worksheetNames == ["[WORKSHEET1]", "[WORKSHEET2]"] # get worksheets (initial response) dataFrameGroup = ts.getDashboard() dataFrameGroup = dataFrameGroup.getWorksheets() assert type(dataFrameGroup) is TableauDashboard assert not dataFrameGroup.cmdResponse assert len(dataFrameGroup.worksheets) == 2 # get worksheets (vql response) dataFrameGroup = dataFrameGroup.worksheets[0].select("[FIELD1]", "2") dataFrameGroup = dataFrameGroup.getWorksheets() assert type(dataFrameGroup) is TableauDashboard assert dataFrameGroup.cmdResponse assert len(dataFrameGroup.worksheets) == 1 # get single worksheet (initial response) dataFrameGroup = ts.getDashboard() dataFrame = dataFrameGroup.getWorksheet("[WORKSHEET1]") assert type(dataFrame) is TableauWorksheet assert not dataFrameGroup.cmdResponse assert len(dataFrameGroup.worksheets) == 2 # get single worksheet (vql response) dataFrameGroup = ts.getDashboard() dataFrame = ( dataFrameGroup.worksheets[0] .select("[FIELD1]", "2") .getWorksheet("[WORKSHEET1]") ) assert type(dataFrame) is TableauWorksheet assert dataFrame.cmdResponse assert dataFrame.name == "[WORKSHEET1]" assert dataFrame.data.shape[0] == 4 assert dataFrame.data.shape[1] == 2 # get single worksheet (vql response) wrong sheet name dataFrameGroup = ts.getDashboard() dataFrame = ( dataFrameGroup.worksheets[0].select("[FIELD1]", "2").getWorksheet("XXXX") ) assert type(dataFrame) is TableauWorksheet assert dataFrame.cmdResponse assert dataFrame.name == "XXXX" assert dataFrame.data.shape[0] == 0 assert dataFrame.data.shape[1] == 0 # get single worksheet (vql response) no data mocker.patch("tableauscraper.api.select", return_value=vqlCmdResponseEmptyValues) dataFrameGroup = ts.getDashboard() dataFrame = ( dataFrameGroup.worksheets[0] .select("[FIELD1]", "2") .getWorksheet("[WORKSHEET1]") ) assert type(dataFrame) is TableauWorksheet assert dataFrame.cmdResponse assert dataFrame.name == "[WORKSHEET1]" assert dataFrame.data.shape[0] == 0 assert dataFrame.data.shape[1] == 0
def get_worksheets(): ts = TS() ts.loads(URL) dashboard = ts.getDashboard() return dashboard.worksheets