【PythonStock(41)】Python全栈股票系统8年项目:3.0.1增加 sqlite 支持,将前端代码合并到了后端简化部署,拆分docker-compose按需拆分文件,默认最简模式

06-01 1261阅读

目录

  • 前言
  • 1,关于PythonStock项目,20250412更新
  • 2,增加 sqlite 支持
  • 3,启动方式使用docker-compose
  • 4,sqlite 数据库字段,和mysql类似但有差异
  • 5,总结

前言


使用Python开发一个web股票项目。
【github项目地址】:
https://gitee.com/pythonstock/stock
https://github.com/pythonstock/stock
【知乎专栏地址】:
https://zhuanlan.zhihu.com/pythonstock
【docker hub地址下载】:
https://hub.docker.com/r/pythonstock/pythonstock
【相关stock资料分类】:
http://blog.csdn.net/freewebsys/article/category/7076584
主要使用开发语言是python。
使用的lib库是pandas numpy sqlalchemy akshare stockstats bokeh等。

本文的原文连接是: https://blog.csdn.net/freewebsys/article/details/108191889

1,关于PythonStock项目,20250412更新


股票视频地址:

pythonstock开源股票系统(5):3.0.1增加 sqlite 支持,将前端代码合并到了后端,拆分docker-compose简化部署

2025-04-12更新
feat: 修改 logo 文件 修改路径。
feat: 增加 静态文件,最简单模式。支持 sqlite 版本。
feat: 增加 sqlite 支持,初始化数据库表结构。
feat: 增加后端单独启动。去掉 nginx,精简模式支持,只部署后端代码,将前端代码合并到了后端,拆分docker-compose:
默认:docker-compose.yml 后端+mysql
开发版本:dev-docker-compose.yml 后端+前端+mysql
前后端拆分:docker-compose-nginx.yml 后端+nginx+前端编译+mysql
sqlite支持:docker-compose-sqlite.yml 后端+sqlite

【PythonStock(41)】Python全栈股票系统8年项目:3.0.1增加 sqlite 支持,将前端代码合并到了后端简化部署,拆分docker-compose按需拆分文件,默认最简模式

主要使用的库是:pandas numpy sqlalchemy akshare stockstats bokeh
2.0 主要做的是迁移了 akshare 库。3.0 主要做的是项目整合,前端使用vue开发了。
项目地址:
https://gitee.com/pythonstock/stock

2,增加 sqlite 支持


https://github.com/ai-rex/torndb-sqlite3
sqlite 支持

#!/usr/bin/python
import sqlite3
# 项目地址:https://github.com/ai-rex/torndb-sqlite3/blob/master/sqlite.py
# 只有一个文件,集成到项目中即可。返回的是一个对象。
#
class Connection(object):
  """A lightweight wrapper around sqlite3; based on tornado.database
  
  db = sqlite.Connection("filename")
  for article in db.query("SELECT * FROM articles")
    print article.title
    
  Cursors are hidden by the implementation.
  """
  
  def __init__(self, filename, isolation_level=None):
    self.filename = filename
    self.isolation_level = isolation_level # None = autocommit
    self._db = None
    try:
      self.reconnect()
    except:
      # log error @@@
      raise
      
  def close(self):
    """Close database connection"""
    if getattr(self, "_db", None) is not None:
      self._db.close()
    self._db = None
      
  def reconnect(self):
    """Closes the existing database connection and re-opens it."""
    self.close()
    self._db = sqlite3.connect(self.filename)
    self._db.isolation_level = self.isolation_level
  
  def cursor(self):
    """Returns the cursor; reconnects if disconnected."""
    if self._db is None: self.reconnect()
    return self._db.cursor()
    
  def __del__(self):
    self.close()
    
  def execute(self, query, *parameters):
    """Executes the given query, returning the lastrowid from the query."""
    cursor = self.cursor()
    try:
      self._execute(cursor, query, parameters)
      return cursor.lastrowid
    finally:
      cursor.close()
      
  def executemany(self, query, parameters):
    """Executes the given query against all the given param sequences"""
    cursor = self.cursor()
    try:
      cursor.executemany(query, parameters)
      return cursor.lastrowid
    finally:
      cursor.close()
      
  def _execute(self, cursor, query, parameters):
    try: 
      return cursor.execute(query, parameters)
    except OperationalError:
      # log error @@@
      self.close()
      raise
      
  def query(self, query, *parameters):
    """Returns a row list for the given query and parameters."""
    cursor = self.cursor()
    try:
      self._execute(cursor, query, parameters)
      column_names = [d[0] for d in cursor.description]
      return [Row(zip(column_names, row)) for row in cursor]
    finally:
      # cursor.close()
      pass
      
  def get(self, query, *parameters):
    """Returns the first row returned for the given query."""
    rows = self.query(query, *parameters)
    if not rows:
      return None
    elif len(rows) > 1:
      raise Exception("Multiple rows returned from sqlite.get() query")
    else:
      return rows[0]
      
class Row(dict):
  """A dict that allows for object-like property access syntax."""
  def __getattr__(self, name):
    try:
      return self[name]
    except KeyError:
      raise AttributeError(name)
      
OperationalError = sqlite3.OperationalError

3,启动方式使用docker-compose


启动方式变了,都切换成了docker-compose,同时镜像切换成了daocloud.io 源。
主要是网络的问题,不使用国外的了,速度快。

使用的基础镜像:

docker.m.daocloud.io/library/mysql:8
docker.m.daocloud.io/library/python:3.11-slim-bullseye
docker.m.daocloud.io/library/node:23.5.0-bullseye-slim
# node:bullseye-slim 有bug 不能升级

默认:dev-docker-compose.yml 后端+mysql
开发版本:dev-docker-compose.yml 后端+前端+mysql
前后端拆分:docker-compose-nginx.yml 后端+nginx+前端编译+mysql
sqlite支持:docker-compose-sqlite.yml 后端+sqlite

networks:
  stock-dev-network:
    driver: bridge
version: "3"
services:
    backend:
        image: pythonstock/backend-dev:latest
        build:
            context: .
            dockerfile: docker/DevBackendDockerfile
        container_name: backend
        ports:
            - "8888:8888"
            - "9090:9090"
        volumes:
        # 设置开发目录,方便开发调试
            - "../backend/jobs/crontab:/var/spool/cron/crontabs/root"
            - "../backend/jobs/cron.minutely:/etc/cron.minutely"
            - "../backend/jobs/cron.hourly:/etc/cron.hourly"
            - "../backend/jobs/cron.daily:/etc/cron.daily"
            - "../backend/jobs/cron.monthly:/etc/cron.monthly"
            - "../backend:/data/stock"
            - "../backend/supervisor:/data/supervisor"
            - "./data/notebooks:/data/notebooks"
            - "./data/logs:/data/logs"
        environment:
            MYSQL_HOST: mysql-stock
            MYSQL_USER:
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。

相关阅读

目录[+]

取消
微信二维码
微信二维码
支付宝二维码