Format
Plain text
Post date
2019-05-19 19:37
Zeitraum der Veröffentlichung
Unbegrenzt
  1. C:\Django\kosu>for /r c:\django\kosu %i in (*.py) do type %i
  2. C:\Django\kosu>type c:\django\kosu\admin.py
  3. from django.contrib import admin
  4. # Register your models here.
  5. C:\Django\kosu>type c:\django\kosu\apps.py
  6. from django.apps import AppConfig
  7. class KosuConfig(AppConfig):
  8. name = 'kosu'
  9. C:\Django\kosu>type c:\django\kosu\forms.py
  10. from django import forms
  11. from .models import uploadModel
  12. from django.core.files.storage import default_storage
  13. class uploadForm(forms.ModelForm):
  14. class Meta:
  15. model = uploadModel
  16. fields = ('attach',)
  17. app_name = 'kosu'
  18. #widgets = {
  19. # 'attach': forms.FileField(),
  20. # }
  21. def getFileName(self):
  22. upload_file = self.cleaned_data['attach']
  23. file_name = default_storage.save(upload_file.name, upload_file)
  24. return default_storage.url(file_name)
  25. C:\Django\kosu>type c:\django\kosu\models.py
  26. from django.db import models
  27. from django.core.validators import FileExtensionValidator
  28. # Create your models here.
  29. class uploadModel(models.Model):
  30. attach = models.FileField(
  31. upload_to='uploads/',
  32. verbose_name='豺サ莉倥ヵ繧。繧、繝ォ',
  33. validators=[FileExtensionValidator(['csv', ])],
  34. )
  35. def __str__(self):
  36. return self.name
  37. C:\Django\kosu>type c:\django\kosu\tests.py
  38. from django.test import TestCase
  39. # Create your tests here.
  40. C:\Django\kosu>type c:\django\kosu\urls.py
  41. from django.urls import path
  42. from kosu.views import kosuView
  43. from django.conf.urls import *
  44. app_name = 'kosu'
  45. urlpatterns = [
  46. path('csv/', kosuView.upload, name='upload'), # 讀懃エ「
  47. url(r'^view_plot/', kosuView.view_plot, name='view_plot'),
  48. ]
  49. C:\Django\kosu>type c:\django\kosu\views.py
  50. from django.shortcuts import render,redirect
  51. import os, csv
  52. from .models import uploadModel
  53. from .forms import uploadForm
  54. import glob
  55. from django.http import HttpResponse
  56. import io
  57. import matplotlib.pyplot as plt
  58. import numpy as np
  59. class kosuView():
  60. app_name = 'kosu'
  61. def upload(request):
  62. form = None
  63. csv = None
  64. if request.method == 'POST':
  65. form = uploadForm(request.POST, request.FILES, instance=uploadModel()) # POST 縺輔l縺・request 繝・・繧ソ縺九i繝輔か繝シ繝繧剃ス懈・
  66. if form.is_valid():
  67. kosuView.deleteFiles(kosuView)
  68. form.save()
  69. csv = form.getFileName()
  70. kosuView.readCsv(kosuView, wkCsv=csv)
  71. #return redirect('index')
  72. else: # GET 縺ョ譎・
  73. form = uploadForm(instance=uploadModel()) # book 繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺九i繝輔か繝シ繝繧剃ス懈・
  74. #time.sleep(4)
  75. #kosuView.get_svg(request, kosuView)
  76. return render(request, 'kosu/upload.html', dict(form=form))#, logModelList=logModelList))
  77. def deleteFiles(self):
  78. files = glob.glob("C:/Django/media/uploads/*")
  79. for f in files:
  80. os.remove(f)
  81. def readCsv(self, wkCsv):
  82. csv_file = open("C:/Django" + wkCsv, "r", encoding="ms932", errors="", newline="")
  83. f = csv.reader(csv_file, delimiter=",", doublequote=True, lineterminator="\r\n", quotechar='"',
  84. skipinitialspace=True)
  85. header = next(f)
  86. print(header)
  87. for row in f:
  88. # row縺ッList
  89. # row[0]縺ァ蠢・ヲ√↑鬆・岼繧貞叙蠕励☆繧九%縺ィ縺後〒縺阪k
  90. print(row)
  91. # 繧ー繝ゥ繝穂ス懈・
  92. def setPlt(self):
  93. # 謚倥l邱壹げ繝ゥ繝輔r蜃コ蜉・
  94. # TODO: 譛ャ蠖薙・pk繧貞渕縺ォ縺励※繝「繝・Ν縺九i繝・・繧ソ繧貞叙蠕励☆繧九€・
  95. x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
  96. y = np.array([20, 90, 50, 30, 100, 80, 10, 60, 40, 70])
  97. plt.plot(x, y)
  98. # svg縺ク縺ョ螟画鋤
  99. def pltToSvg(self):
  100. buf = io.BytesIO()
  101. plt.savefig(buf, format='svg', bbox_inches='tight')
  102. s = buf.getvalue()
  103. buf.close()
  104. return s
  105. def view_plot(self, request):
  106. kosuView.setPlt(kosuView) # create the plot
  107. svg = kosuView.pltToSvg(kosuView) # convert plot to SVG
  108. plt.cla() # clean up plt so it can be re-used
  109. response = HttpResponse(svg, content_type='image/svg+xml')
  110. return response
  111. C:\Django\kosu>type c:\django\kosu\__init__.py
  112. C:\Django\kosu>type c:\django\kosu\migrations\0001_initial.py
  113. # Generated by Django 2.2 on 2019-05-18 06:45
  114. import django.core.validators
  115. from django.db import migrations, models
  116. class Migration(migrations.Migration):
  117. initial = True
  118. dependencies = [
  119. ]
  120. operations = [
  121. migrations.CreateModel(
  122. name='uploadModel',
  123. fields=[
  124. ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
  125. ('attach', models.FileField(upload_to='uploads/', validators=[django.core.validators.FileExtensionValidator(['csv'])], verbose_name='豺サ莉倥ヵ繧。繧、繝ォ')),
  126. ],
  127. ),
  128. ]
  129. C:\Django\kosu>type c:\django\kosu\migrations\__init__.py
  130. C:\Django\kosu>
Download Printable view

URL of this paste

Embed with JavaScript

Embed with iframe

Raw text