Use Python to automatically create a promotional image for today in history

Hits: 0

Hello everyone, today I will share a tutorial on automatically creating related pictures through Python, and this related picture is “Today in History”, so why is it history? With the increase of age, this enthusiasm has not diminished~

Okay, let’s not talk about gossip, let’s go straight to the dry goods

data collection

The first is where the data comes from. I tried to use some free historical query interfaces on the Internet, but the results were not satisfactory. These interfaces are either too unstable or the data is not friendly. In the end, I chose a streamlined website, and I can directly scrape the website page information.

The website is very simple, and there are no anti-crawling measures, we directly grab the data

The variables of month and day are provided here, so that we can obtain historical information at any time when we make a web service later.

Then we write a function to convert the obtained data into DataFrame format

picture making

First of all, we make it clear that the basis of the picture is the Line type, which is the line chart we usually use the most!

Generate Y-axis data first

pyecharts.optionsUse the class in the series configuration LineItem, but it is very strange that this class can’t be found on the official website of PyEcharts, or you have to check the official source code to understand its function.

class LineItem(BasicOpts):    def __init__(        self,        name: Union[str, Numeric] = None,        value: Union[str, Numeric] = None,        *,        symbol: Optional[str] = "circle",        symbol_size: Numeric = 4,        symbol_rotate: Optional[Numeric] = None,        symbol_keep_aspect: bool = False,        symbol_offset: Optional[Sequence] = None,        label_opts: Union[LabelOpts, dict, None] = None,        itemstyle_opts: Union[ItemStyleOpts, dict, None] = None,        tooltip_opts: Union[TooltipOpts, dict, None] = None,    ):        self.opts: dict = {            "name": name,            "value": value,            "symbol": symbol,            "symbolSize": symbol_size,            "symbolRotate": symbol_rotate,            "symbolKeepAspect": symbol_keep_aspect,            "symbolOffset": symbol_offset,            "label": label_opts,            "itemStyle": itemstyle_opts,            "tooltip": tooltip_opts,        }

The general meaning is to set the properties of Line in batches. Here I have to complain about the official documentation of PyEcharts, which should be properly maintained~ (If I understand it wrong here, please point out, let’s learn together~)

That is to say, the above code generates a series of data. The X axis of these data is 10, and the Y axis starts from 0 and increases until the last value of the loop. At the same time, LabelOptsthe msg information is also set, that is, we finally see historical information

After the XY axis data is set, other styles are adjusted.

The test here is the proficiency of PyEcharts. Anyway, I am not up to the standard of radish. If this style is my own, I may have to adjust it according to the official website for a long time, hahaha

Well, the picture production is introduced here

Deploy the web service

Because there is a need to get pictures every day, and then forward them to the WeChat group, the most convenient way is to deploy it as a Web and access it on the public Internet.

For this kind of temporary, personal website, it is recommended to use Flask, after all, fast is the advantage (fast here refers to writing fast, getting started quickly~)

Import Flask and PyEcharts related libraries

from  flask  import  Flaskfrom jinja2  import  Markup, Environment, FileSystemLoaderfrom pyecharts.globals  import  CurrentConfigimport datetimefrom flask  import  request # For CurrentConfig, please refer to [Basic Use-Global Variables]CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader("./templates"))

Then set the routing function

def gen_line(month, day):    result_dict, day = get_data(month, day)    df = gen_df(result_dict)    y = gen_y(df)    line = myLine(y, day)    return line@app.route("/")def index():    month = request.args.get("month")    day = request.args.get("day")    if month and day:        c = gen_line(month, day)        return Markup(c.render_embed())    i =    c = gen_line(i.month,    return Markup(c.render_embed())

That’s good, just start it through the web container that comes with Flask

if __name__ == "__main__":, host="")

Well, today’s sharing is here, students who want to experience, you can visit this website

See the friends here, if you feel satisfied, please give a thumbs up + watch support.

One-click three links, private message to get the complete code~

This article is published by mdnice Multiplatform

You may also like...

Leave a Reply

Your email address will not be published.