Samples

 

The complete code sample can be found on GitHub.

 

 

 

To use the reporting tool, just install stimulsoft-reports or stimulsoft-dashboards package using the package installer by running the following command:

 

console

 

python -m pip install stimulsoft-reports

 

 

console

 

python -m pip install stimulsoft-dashboards

 

 

 

The latest available version of the reporting tool will be installed in the current workspace, after which you can use classes and functions for working with reports.

 

Information

 

For code examples, the Flask framework is used as one of the most popular and easy-to-understand frameworks. You can use any web framework because all classes and functions for working with the reporting tool are completely independent.

 

 

 

The StiReport class is designed to work with a reporting tool in a web project. Using this class, you can create a report, load a report from a file or a line, build a report, or export a report. For example, if you need to load a report from a file, build it, and export it to HTML:

 

app.py

 

from flask import Flask, render_template, url_for, request

from stimulsoft_reports.report import StiReport

from stimulsoft_reports.report.enums import StiExportFormat

 

app = Flask(__name__)

 

@app.route('/report', methods = ['GET', 'POST'])

def report():

   report = StiReport()

  if report.processRequest(request):

      return report.getFrameworkResponse()

 

   report.loadFile(url_for('static', filename='reports/SimpleList.mrt'))

   report.render()

   report.exportDocument(StiExportFormat.HTML)

 

   js = report.javascript.getHtml()

   html = report.getHtml()

  return render_template('report.html', reportJavaScript = js, reportHtml = html)

 

 

index.html

 

<!DOCTYPE html>

<html>

 

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Render and Export a Report</title>

 

{{ reportJavaScript|safe }}

</head>

 

<body>

{{ reportHtml|safe }}

</body>

 

</html>

 

 

 

In this example, an instance of the StiReport object is created, a report template is loaded from the SimpleList.mrt file located in the static files directory, and the command to build and export the report is called. Next, the JavaScript and HTML code of the components necessary for the operation of the reporting tool are generated. The report.javascript.getHtml() function generates HTML code for connecting the necessary resources, while the report.getHtml() function generates HTML code for working with the reporting tool. The generated HTML code is passed as parameters to the HTML template report.html, where it is displayed in the required places.

 

The special function report.processRequest(request) processes the current request. If it returns True, the request is intended for the reporting tool, and you need to return the result of its execution instead of the page template. This is described in more detail in a separate section of the documentation.

 

Information

 

Our products Stimulsoft Reports.PYTHON and Stimulsoft Dashboards.PYTHON do not have a native Python report engine. Report generation and export are performed on the client side using JavaScript code. Therefore, when using Python code to work with components, you should call the getHtml() function, which will return all the necessary JavaScript code to add to the web page.

 

 

 

Framework support

Currently, there is built-in support for the three main Python frameworks - Flask, Django, and Tornado. Universal functions for working in any other frameworks are also available. For code examples, the Flask framework was used, as it is one of the most popular and easy-to-understand codes; product deployment for it was discussed above. Below are examples of the same code for other frameworks. They are all very similar and differ only in the features used for a specific framework.

 

Django

 

app.py

 

from django.shortcuts import render

from django.templatetags.static import static

from stimulsoft_reports.report import StiReport

from stimulsoft_reports.report.enums import StiExportFormat

 

def report(request):

   report = StiReport()

  if report.processRequest(request):

      return report.getFrameworkResponse()

 

   report.loadFile(static('reports/SimpleList.mrt'))

   report.render()

   report.exportDocument(StiExportFormat.HTML)

 

   js = report.javascript.getHtml()

   html = report.getHtml()

  return render(request, 'report.html', {'reportJavaScript': js, 'reportHtml': html})

 

 

 

report.html

 

<!DOCTYPE html>

<html>

 

<head>

   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

   <title>Render and Export a Report</title>

 

  {{ reportJavaScript|safe }}

</head>

 

<body>

  {{ reportHtml|safe }}

</body>

 

</html>

 

 

 

Tornado

 

app.py

 

import asyncio, os

from tornado.web import Application, RequestHandler, url

from stimulsoft_reports.report import StiReport

from stimulsoft_reports.report.enums import StiExportFormat

 

class ReportHandler(RequestHandler):

  def get(self):

       report = StiReport()

      if report.processRequest(request):

          return report.getFrameworkResponse(self)

 

       report.loadFile(self.static_url('reports/SimpleList.mrt'))

       report.render()

       report.exportDocument(StiExportFormat.HTML)

 

       js = report.javascript.getHtml()

       html = report.getHtml()

       self.render('report.html', reportJavaScript = js, reportHtml = html)

 

  def post(self):

       handler = StiHandler()

      if handler.processRequest(self.request):

          return handler.getFrameworkResponse(self)

 

 

report.html

 

<!DOCTYPE html>

<html>

 

<head>

   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

   <title>Render and Export a Report</title>

 

  {% raw reportJavaScript %}

</head>

 

<body>

  {% raw reportHtml %}

</body>

 

</html>

 

 

 

Universal function

 

app.py

 

from stimulsoft_reports.report import StiReport

from stimulsoft_reports.report.enums import StiExportFormat

 

def report():

   report = StiReport()

   query = 'query string'

   body = 'post data'

  if report.processRequest(None, query, body):

       response = report.getResponse()

       data = response.data

       contentType = response.contentType

       mimetype = response.mimetype

 

   report.loadFile(url_for('static', filename='reports/SimpleList.mrt'))

   report.render()

   report.exportDocument(StiExportFormat.HTML)

 

   js = report.javascript.getHtml()

   html = report.getHtml()

 

 

Information

 

In most cases, to work with the product, you should use only Python code, which provides interaction with all the main features. To configure the product more precisely and utilize all the capabilities of the JS reporting software, you must use JavaScript code. The option to deploy a product using only JavaScript code is described in the Reports and Dashboards for JS section. In this case, the use of Python code is required only to connect data adapters.

 

 

 

Various deployment and optimization options are discussed in the section Optimizing scripts loading.