vue.js python

If your organization does not have a data visualization solution like Tableau or PowerBI nor means to host a server to deploy open source solutions like Dash then you are probably stuck doing reports with Excel or exporting your notebooks.

如果您的组织没有Tableau或PowerBI这样的数据可视化解决方案,也没有托管服务器来部署Dash这样的开源解决方案的主机,那么您可能就无法使用Excel进行报告或导出笔记本。

In this post, I will walk you through an example of how you can do reports using Python and Vue.js and send them by email to stakeholders.

在本文中,我将为您提供一个示例,说明如何使用Python和Vue.js生成报告并通过电子邮件将其发送给涉众。

过程概述 (Overview of the process)

First, let’s see from a global point of view the process we are trying to automate. Reporting process generally consists of :

首先,让我们从全局的角度来看我们要自动化的过程。 报告过程通常包括:

  1. Pulling data from one or multiple sources从一个或多个来源提取数据
  2. Updating a template report with the new data使用新数据更新模板报告
  3. Send notification or newly created report to stakeholders向利益相关者发送通知或新创建的报告

We are going to create an HTML file that will contain our report’s template then using python and the jinja2 library we are going to inject data into our template to create an instance of our report as a static HTML file. Finally, we’ll send our static HTML file by email.

我们将创建一个包含报告模板的HTML文件 ,然后使用python和jinja2库 将数据注入模板中,以静态HTML文件形式创建报告实例。 最后,我们将通过电子邮件发送静态HTML文件。

要求 (Requirements)

Before we get started, you will need to have Python 3.7 or greater installed and a google account. The final report is using some ES6 features that are not supported by old browsers so make sure to use a modern web browser. Finally, we’ll be using the jinja2 library so you’ll need to install it with : pip install Jinja2 .

在开始之前,您需要安装Python 3.7或更高版本以及google帐户 。 最终报告使用的是旧浏览器不支持的某些ES6功能 ,因此请确保使用现代的Web浏览器 。 最后,我们将使用jinja2库,因此您需要使用以下命令进行安装: pip install Jinja2

创建模板引擎 (Creating the template engine)

We first start by creating a script that will allow us to input a template location, a destination, and data and will output a file to the specified destination with data injected in it. I created a DefaultTemplater class that wraps the process here is the code.

首先,我们创建一个脚本,该脚本将允许我们输入模板位置,目标位置和数据,并将文件输出到指定的目标位置,并在其中插入数据。 我创建了一个DefaultTemplater类该类将过程包装在此处。

Implementation of a templater engine using jinja2.
使用jinja2实现模板引擎。

The DefaultTemplater class is created with a template location and an output destination. The important piece here is the replace method, it uses the Template class from jinja2 to search for tags into the template. Tags are named markup made inside the template file that tells jinja2 to replace the markup with some value. Let’s test our script.

使用模板位置和输出目标创建DefaultTemplater类 。 这里的重要部分是replace方法,它使用jinja2中Template类模板中搜索标签。 标签是在模板文件中创建的名为标记的标记,告诉jinja2将标记替换为某些值。 让我们测试一下脚本。

  • Create a text file and put in it a tag : {{test}}

    创建一个文本文件,并在其中放入标签: {{test}}

  • Create an app.py python file and copy/paste the code below

    创建一个app.py python文件并复制/粘贴下面的代码

Testing the DefaultTemplater class.
测试DefaultTemplater类。
  • Change <you_path> with your text file path;用您的文本文件路径更改<you_path>;
  • Run the app.py file运行app.py文件
  • Look in the templated.txt you should see that the markup {{test}} has been replaced by Hello world.

    查看templated.txt,您应该看到标记{{test}}已被Hello world取代。

Now that our template engine is ready, we need to create the report’s template.

现在我们的模板引擎已经准备就绪,我们需要创建报告的模板。

使用Vue.js创建报告的模板 (Creating the report’s template using Vue.js)

In this part, we will create an HTML page with the Vue.js Framework. It will display a basic bar chart representing some fake sales.

在这一部分中,我们将使用Vue.js框架创建一个HTML页面。 它将显示代表一些假销售的基本条形图。

If you don’t know what Vue.js is I’ll recommend going and read the official website that is great. Basically it’s a progressive javascript Framework, it means you are not forced to used build tools like Webpack, npm. In our example, we will use Vue simply by adding it in a script tag. In addition, Vue simplifies the workflow of interacting with the DOM.

如果您不知道Vue.js是什么,我建议您去阅读一下很棒的官方网站 。 基本上,它是一个渐进式javascript框架,这意味着您不必强制使用Webpack,npm等构建工具。 在我们的示例中,我们将简单地通过将Vue添加到脚本标签中来使用它。 此外,Vue简化了与DOM交互的工作流程。

Finally, we will be using some libraries that are specific to Vue :

最后,我们将使用一些特定于Vue的库:

  • Vuetify: It will allow us to use their material design components out of the box. We will have a little configuration to do in order to have a nice looking page.

    Vuetify :它将使我们可以立即使用其材料设计组件。 我们将做一些配置以使页面看起来不错。

  • v-chart: A wrapper library around apache Echarts that will make our life easier in creating interactive charts.

    v-chart :围绕Apache Echarts的包装器库,这将使我们的生活更轻松地创建交互式图表。

Now that we have an overview of the tooling let’s create a template.html file :

现在,我们对工具进行了概述,让我们创建一个template.html文件:

Template for creating a simple sales histogram.
用于创建简单销售直方图的模板。

The template is quite basic :

该模板非常基本:

  1. We start by loading some CSS styles (lines 3 to 6);我们首先加载一些CSS样式(第3至6行);
  2. In the body, we create a div tag that will host our Vue app (line 11);在主体中,我们创建一个div标签,该标签将托管我们的Vue应用(第11行);
  3. Inside the div, we make use of Vuetify components;在div内部,我们使用Vuetify组件;
  4. We bind data value and settings property from the ve-histogram component (line 30). It means that any changes made to chartData and chartSetting will be propagated;我们绑定ve直方图组件的数据值和settings属性(第30行)。 这意味着对chartData和chartSetting所做的任何更改都将传播。
  5. We load libraries Vue, Vuetify and Echarts (lines 44–50);我们加载库Vue,Vuetify和Echarts(第44-50行);
  6. Finally, inside the last script tag ( lines 52–72) reside our Vue app. The Vue app is made of a data function that returns some property that we can use in the HTML section. We have defined 3 objects that are used in HTML, one for display the date, one for the chart data, and one for the settings of the chart. We attach to each of these 3 objects a jinja2 markups so that our python script will replace them with real data.最后,在最后一个脚本标签(第52–72行)中包含我们的Vue应用程序。 Vue应用程序由数据函数组成,该函数返回一些我们可以在HTML部分中使用的属性。 我们定义了3个在HTML中使用的对象,一个用于显示日期,一个用于图表数据,一个用于图表设置。 我们将jinja2标记附加到这3个对象中的每一个上,以便我们的python脚本将其替换为真实数据。

If you are not familiar with Front end technologies such as Javascript, HTML it might be hard to understand the code. In this post I won’t go deep into Vue.js mechanisms, the official tutorial is a good starting point. So make sure to take a look at it if you have trouble understanding the code.

如果您不熟悉Java,HTML等前端技术,则可能很难理解代码。 在本文中,我不会深入研究Vue.js机制, 官方教程是一个很好的起点。 因此,如果您在理解代码方面遇到困难,请务必仔细阅读。

Now let’s write code to send an email with an attachment.

现在,让我们编写代码以发送带有附件的电子邮件。

创建代码以发送带有附件的电子邮件 (Creating the code to send an email with an attachment)

In this example, I will be using Gmail. In order to interact with Gmail from a script, you’ll need to configure your Google account. Fortunately, there is a great medium post that will walk you through how to configure your google account.

在此示例中,我将使用Gmail。 为了通过脚本与Gmail进行交互,您需要配置Google帐户。 幸运的是,有一篇很棒的媒体文章将引导您完成如何配置您的Google帐户。

We could have used the yagmail library as the story shows us in the first part however while testing it, it is not possible to send an HTML extension file as an attachment see GitHub issue here.

我们本可以使用yagmail库 正如故事在第一部分中向我们展示的那样,但是在对其进行测试时,无法将HTML扩展文件作为附件发送,请参见GitHub issue here 。

So, we’ll need to create a script to send an email with the standard library as the related story shows us in the second part. I started from the code the story’s author provided and wrapped it into a class. Here is the result:

因此,我们需要创建一个脚本来发送带有标准库的电子邮件,因为相关故事在第二部分中向我们展示。 我从故事作者提供的代码开始,并将其包装到一个类中。 结果如下:

Wrapper class around standard libraries' email utilities.
包装库类围绕标准库的电子邮件实用程序。

The Gmail class connects to the Gmail SMTP server with provided credentials when a new object is created. It’s possible to use context manager as special methods __enter__ and __exit__ are implemented. Finally, the send method allows us to send an email with or without attachment.

创建新对象时, Gmail类将使用提供的凭据连接到Gmail SMTP服务器。 可以使用上下文管理器作为实现__enter____exit__的特殊方法。 最后,send方法允许我们发送带有或不带有附件的电子邮件。

Now that our Gmail class is written, let’s put all our components to work together in order to automate our reporting process.

既然我们已经编写了Gmail类,那么让我们将所有组件放在一起使用,以使报告过程自动化。

将所有内容放在一起以创建最终报告 (Putting it all together to create the final report)

In this example, I will simply use a list of dicts as data for the report. However, it could have been a pandas data frame fetching data from an SQL database. So let’s look at the final code that will allow us to automate our reporting. You will need to put in your Google credentials in order to work.

在此示例中,我将仅使用字典列表作为报告数据。 但是,它可能是从SQL数据库中获取数据的熊猫数据框。 因此,让我们看一下将使我们能够自动执行报告的最终代码。 您需要输入Google凭据才能正常工作。

Final code to automate the reporting process.
最终代码可自动执行报告过程。

Launch the script and if you go to your Gmail account you should see that the email was sent by looking in the email sent section. If you download the report.html attached to it and open it with chrome you should see this :

启动脚本,如果您转到Gmail帐户,则可以通过在“电子邮件已发送”部分中查看该电子邮件已发送。 如果下载附件中的report.html并使用chrome打开它,则应该看到以下内容:

Sales report created with the script.
使用脚本创建的销售报告。

Thanks to Python and Vue.js we were able to produce a yet simple histogram, but more importantly, we have configured the basics to automate the reporting process.

多亏了Python和Vue.js,我们能够生成一个非常简单的直方图,但是更重要的是,我们已经配置了一些基础知识来使报告过程自动化。

然后去哪儿? (Where to go from here?)

Now that the basics are set up, you could improve your template in order to make your report look greater. For instance, you can use advanced features from Echarts library to implement a drill-down drill-up feature into your report.

现在已经建立了基础,您可以改进模板以使报告看起来更大。 例如,您可以使用E 图表库中的高级功能在报表中实现向下追溯功能。

Drill down drill up feature.
向下钻取向上功能。

If you are not familiar with web technologies such as javascript or HTML, you will have to learn them in order to produce advanced reports. But once learned, the sky is the limit in terms of customizing your reports, especially thanks to the rich javascript ecosystem.

如果您不熟悉javascript或HTML之类的网络技术,则必须学习它们才能生成高级报告。 但是,一旦了解到,自定义报告就成为了限制,特别是由于丰富的javascript生态系统。

缺点和其他考虑 (Drawbacks & Other considerations)

I want to draw attention to some important points before concluding this story. First, the reporting solution presented here is designed to display a small amount of data because of the limitations of web browsers available memory and by email size.

在结束这个故事之前,我想提请注意一些重要的观点。 首先,由于Web 浏览器的可用内存和电子邮件大小的限制,此处介绍的报告解决方案旨在显示少量数据。

Second, be aware that data injected is shipped with the report so make sure to take care of data confidentiality before starting to use a solution like this.

其次,请注意,注入的数据是随报告一起提供的,因此在开始使用这种解决方案之前,请确保对数据保密。

Finally, always prefer using solutions that are pushed by your team or your company.

最后,始终喜欢使用团队或公司推动的解决方案。

结论 (Conclusion)

As a Data analyst, my daily job involves reporting tasks in order to communicate on key business metrics. Being able to automate some of them has saved me a lot of time although it took me a while to set it up.

作为数据分析师,我的日常工作涉及报告任务,以便就关键业务指标进行沟通。 能够使其中一些自动化使我节省了大量时间,尽管我花了一些时间进行设置。

I hope you enjoyed reading this post and learn something useful. Any constructive feedback, suggestions, or code improvements will be appreciated. Thank you for reading.

我希望您喜欢阅读这篇文章并学到一些有用的东西。 任何建设性的反馈,建议或代码改进将不胜感激。 感谢您的阅读。

翻译自: https://medium.com/swlh/automate-your-reporting-with-python-and-vue-js-15ef130fff8

vue.js python


http://www.taodudu.cc/news/show-995066.html

相关文章:

  • 计算机科学必读书籍_5篇关于数据科学家的产品分类必读文章
  • python 网页编程_通过Python编程检索网页
  • data studio_面向营销人员的Data Studio —报表指南
  • 乐高ev3 读取外部数据_数据就是新乐高
  • java 分裂数字_分裂的补充:超越数字,打印物理可视化
  • 比赛,幸福度_幸福与生活满意度
  • 5分钟内完成胸部CT扫描机器学习
  • openai-gpt_为什么到处都看到GPT-3?
  • 数据可视化及其重要性:Python
  • ai驱动数据安全治理_AI驱动的Web数据收集解决方案的新起点
  • 使用K-Means对美因河畔法兰克福的社区进行聚类
  • 因果关系和相关关系 大数据_数据科学中的相关性与因果关系
  • 分类结果可视化python_可视化分类结果的另一种方法
  • rstudio 管道符号_R中的管道指南
  • 时间序列因果关系_分析具有因果关系的时间序列干预:货币波动
  • 无法从套接字中获取更多数据_数据科学中应引起更多关注的一个组成部分
  • 深度学习数据更换背景_开始学习数据科学的最佳方法是了解其背景
  • 数据中台是下一代大数据_全栈数据科学:下一代数据科学家群体
  • 泰坦尼克数据集预测分析_探索性数据分析-泰坦尼克号数据集案例研究(第二部分)
  • 大数据技术 学习之旅_如何开始您的数据科学之旅?
  • 搜索引擎优化学习原理_如何使用数据科学原理来改善您的搜索引擎优化工作
  • 一件登录facebook_我从Facebook的R教学中学到的6件事
  • python 图表_使用Streamlit-Python将动画图表添加到仪表板
  • Lockdown Wheelie项目
  • 实现klib_使用klib加速数据清理和预处理
  • 简明易懂的c#入门指南_统计假设检验的简明指南
  • python 工具箱_Python交易工具箱:通过指标子图增强图表
  • python交互式和文件式_使用Python创建和自动化交互式仪表盘
  • 无向图g的邻接矩阵一定是_矩阵是图
  • 熊猫分发_熊猫新手:第一部分

vue.js python_使用Python和Vue.js自动化报告过程相关推荐

  1. python js返回 json_[python爬虫]把js转化成json

    有一个优秀的库可以使用----demjson 目标链接 请求上面链接,会得到如下图的一个js文件 我们需要把这个js文件转成为dict,方便提取其中需要的字段(这在爬虫任务中非常常见) 失败的方法 传 ...

  2. python自动输出_python自动化报告的输出

    1.设计简单的用例 2.设计用例 以TestBaiduLinks.py命名 1 # coding:utf-8 2 3 from selenium import webdriver 4 import u ...

  3. vue使用python_如何使用Python和Vue创建两人游戏

    vue使用python by Neo Ighodaro 由新Ighodaro 如何使用Python和Vue创建两人游戏 (How to create a two-player game with Py ...

  4. vue解决启动报错cjs loader.js Error: Cannot find module ‘../config‘问题

    vue解决启动报错cjs loader.js Error: Cannot find module '../config'问题 参考文章: (1)vue解决启动报错cjs loader.js Error ...

  5. 【vue.js开发】如何在vue里面优雅的解决跨域,路由冲突问题

    [vue.js开发]如何在vue里面优雅的解决跨域,路由冲突问题 当我们在路由里面配置成以下代理可以解决跨域问题 proxyTable: {'/goods/*': {target: 'http://l ...

  6. Vue中向js中传递参数并在js中定义对象并转换参数

    场景 有下面这种主从表结构 上面的信息是主表的信息,下面是从表的信息. 在Vue中将页面的信息传递到js的方法中,在js方法中将参数进行转换使其与后台接收的参数相匹配. 注: 博客: https:// ...

  7. vue 公用页面引用_关于vue全局引用公共的js和公共的组件的折腾

    前沿 最近在项目开发中遇到一些需要全局引用的公共js,或者公共组件,早就烦死了那种每个页面都写一遍,都引用一个js的写法,正好vue解决了这个额问题,于是乎就开始折腾,折腾的过程中也发现了一些自己之前 ...

  8. Vue.js-Day03-AM【超级详细:Node.js环境安装、安装淘宝镜像(Win、Mac)、安装Vue脚手架、初始化Vue项目-命令解释(Vscode、命令行窗口)、目录介绍、Vue文件介绍】

    Vue.js实训[基础理论(5天)+项目实战(5天)]博客汇总表[详细笔记] 目   录 1.Node.js-环境安装 1.1.Node.js-详细安装步骤 2.vue-cli脚手架安装 2.1.安装 ...

  9. 删除vue打包大小限制_压缩Vue.js打包后的体积方法总结(Vue.js打包后体积过大问题)...

    问题 由于这次项目是在初学 Vue 之后的第一个正式项目,没有考虑到类似 路由懒加载. 按需加载的问题 ,所以呢,也算是没经验. 到了这些天,项目写得差不多了,准备放到服务器测试,才发现这个问题. 优 ...

最新文章

  1. Lambda中的常用sql方法
  2. spring源码分析之cache注解
  3. 求两个Linux文本文件的交集、差集、并集
  4. 有生之年必看!千古第一奇书《山海经》到底是怎样的一本书?
  5. Codeforces Round #694 (Div. 2) E. Strange Shuffle 交互 + 思维分块
  6. javafx 动画没效果_通过JavaFX标注制作动画效果
  7. python iloc iat_Python Pandas Dataframe.iat[ ]用法及代码示例
  8. 基于tcp的网络程序_【CVPR 2020 Tutorial】基于神经网络的符号化视觉推理和程序合成(2)...
  9. 深度学习面试的一些知识
  10. 理解 Symbol.toStringTag 用法
  11. python3 urllib模块
  12. python学生可以学吗_如何劝学生别浪费时间学Python
  13. python统计三国演义中人物出现的频次
  14. linux 域名判断 跳转,Nginx判断不同的域名指向不同的root路径实例
  15. 工业云平台大数据统计分析有什么优势?
  16. ESD静电二极管的应用(红外温枪防护)
  17. hadoop103: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
  18. 迪赛推出国家公立医院绩效考核服务系统,融合迪赛智慧数加持赋能,让二三级医院用户领跑“国考”!
  19. FPGA项目承接|FPGA项目外包|FPGA项目研发
  20. 如何设置linux lang环境变量,设置linux环境变量LANG(示例代码)

热门文章

  1. Linux系统编程---5(共享存储映射,存储映射I/O,mmap函数,父子进程间通信,匿名映射)
  2. (C语言版)链表(四)——实现双向循环链表创建、插入、删除、释放内存等简单操作
  3. 【Verilog HDL】语句的并发执行
  4. Pthread创建线程后必须使用join或detach释放线程资源
  5. 【大牛系列教学】java面试常考的编程题
  6. 《HTTP 权威指南》笔记:第十五章 实体与编码
  7. 十 web爬虫讲解2—Scrapy框架爬虫—Scrapy安装—Scrapy指令
  8. 【转】消息队列应用场景
  9. Shell编程 之 for 循环
  10. APUE学习之三个特殊位 设置用户ID(set-user-ID),设置组ID(set-group-ID),sticky...